History

avfilter/af_acrossover: reduce memory usage

Split biquad coefficients from biquad state.

Paul B Mahol committed 5 years ago

Commit c5cdec91 parent 7eb9cf51 file · +42 −33Browse files

Unified · Split

libavfilter/af_acrossover.c+42−33
@@ -39,9 +39,12 @@ · 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
42typedef struct BiquadContext {42typedef struct BiquadCoeffs {
43 double b0, b1, b2;43 double b0, b1, b2;
44 double a1, a2;44 double a1, a2;
45} BiquadCoeffs;
46
47typedef struct BiquadContext {
45 double z1, z2;48 double z1, z2;
46} BiquadContext;49} BiquadContext;
47 50
@@ -65,6 +68,10 @@ typedef struct AudioCrossoverContext { · 17 unmodified lines
65 int nb_splits;68 int nb_splits;
66 float *splits;69 float *splits;
67 70
71 BiquadCoeffs lp[MAX_BANDS][20];
72 BiquadCoeffs hp[MAX_BANDS][20];
73 BiquadCoeffs ap[MAX_BANDS][20];
74
68 CrossoverChannel *xover;75 CrossoverChannel *xover;
69 76
70 AVFrame *input_frame;77 AVFrame *input_frame;
@@ -158,7 +165,7 @@ static av_cold int init(AVFilterContext *ctx) · 87 unmodified lines
158 return ret;165 return ret;
159}166}
160 167
161static void set_lp(BiquadContext *b, double fc, double q, double sr)168static void set_lp(BiquadCoeffs *b, double fc, double q, double sr)
162{169{
163 double omega = M_PI * fc / sr;170 double omega = M_PI * fc / sr;
164 double cosine = cos(omega);171 double cosine = cos(omega);
@@ -178,7 +185,7 @@ static void set_lp(BiquadContext *b, double fc, double q, double sr) · 13 unmodified lines
178 b->a2 = -a2 / a0;185 b->a2 = -a2 / a0;
179}186}
180 187
181static void set_hp(BiquadContext *b, double fc, double q, double sr)188static void set_hp(BiquadCoeffs *b, double fc, double q, double sr)
182{189{
183 double omega = M_PI * fc / sr;190 double omega = M_PI * fc / sr;
184 double cosine = cos(omega);191 double cosine = cos(omega);
@@ -198,7 +205,7 @@ static void set_hp(BiquadContext *b, double fc, double q, double sr) · 13 unmodified lines
198 b->a2 = -a2 / a0;205 b->a2 = -a2 / a0;
199}206}
200 207
201static void set_ap(BiquadContext *b, double fc, double q, double sr)208static void set_ap(BiquadCoeffs *b, double fc, double q, double sr)
202{209{
203 double omega = M_PI * fc / sr;210 double omega = M_PI * fc / sr;
204 double cosine = cos(omega);211 double cosine = cos(omega);
@@ -218,7 +225,7 @@ static void set_ap(BiquadContext *b, double fc, double q, double sr) · 13 unmodified lines
218 b->a2 = -a2 / a0;225 b->a2 = -a2 / a0;
219}226}
220 227
221static void set_ap1(BiquadContext *b, double fc, double sr)228static void set_ap1(BiquadCoeffs *b, double fc, double sr)
222{229{
223 double omega = M_PI * fc / sr;230 double omega = M_PI * fc / sr;
224 231
@@ -268,15 +275,16 @@ static int query_formats(AVFilterContext *ctx) · 43 unmodified lines
268}275}
269 276
270#define BIQUAD_PROCESS(name, type) \277#define BIQUAD_PROCESS(name, type) \
271static void biquad_process_## name(BiquadContext *b, \278static void biquad_process_## name(const BiquadCoeffs *const c,\
279 BiquadContext *b, \
272 type *dst, const type *src, \280 type *dst, const type *src, \
273 int nb_samples) \281 int nb_samples) \
274{ \282{ \
275 const type b0 = b->b0; \283 const type b0 = c->b0; \
276 const type b1 = b->b1; \284 const type b1 = c->b1; \
277 const type b2 = b->b2; \285 const type b2 = c->b2; \
278 const type a1 = b->a1; \286 const type a1 = c->a1; \
279 const type a2 = b->a2; \287 const type a2 = c->a2; \
280 type z1 = b->z1; \288 type z1 = b->z1; \
281 type z2 = b->z2; \289 type z2 = b->z2; \
282 \290 \
@@ -321,16 +329,18 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 38 unmodified lines
321 type *dst = (type *)frames[band + 1]->extended_data[ch]; \329 type *dst = (type *)frames[band + 1]->extended_data[ch]; \
322 const type *hsrc = f == 0 ? prv : dst; \330 const type *hsrc = f == 0 ? prv : dst; \
323 BiquadContext *hp = &xover->hp[band][f]; \331 BiquadContext *hp = &xover->hp[band][f]; \
332 BiquadCoeffs *hpc = &s->hp[band][f]; \
324 \333 \
325 biquad_process_## name(hp, dst, hsrc, nb_samples); \334 biquad_process_## name(hpc, hp, dst, hsrc, nb_samples); \
326 } \335 } \
327 \336 \
328 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \337 for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \
329 type *dst = (type *)frames[band]->extended_data[ch]; \338 type *dst = (type *)frames[band]->extended_data[ch]; \
330 const type *lsrc = dst; \339 const type *lsrc = dst; \
331 BiquadContext *lp = &xover->lp[band][f]; \340 BiquadContext *lp = &xover->lp[band][f]; \
341 BiquadCoeffs *lpc = &s->lp[band][f]; \
332 \342 \
333 biquad_process_## name(lp, dst, lsrc, nb_samples); \343 biquad_process_## name(lpc, lp, dst, lsrc, nb_samples); \
334 } \344 } \
335 \345 \
336 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \346 for (int aband = band + 1; aband + 1 < ctx->nb_outputs; aband++) { \
@@ -338,16 +348,18 @@ static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, i · 1 unmodified line
338 const type *asrc = (const type *)frames[band]->extended_data[ch]; \348 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
339 type *dst = (type *)frames[band]->extended_data[ch]; \349 type *dst = (type *)frames[band]->extended_data[ch]; \
340 BiquadContext *ap = &xover->ap[band][aband][0]; \350 BiquadContext *ap = &xover->ap[band][aband][0]; \
351 BiquadCoeffs *apc = &s->ap[aband][0]; \
341 \352 \
342 biquad_process_## name(ap, dst, asrc, nb_samples); \353 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
343 } \354 } \
344 \355 \
345 for (int f = s->first_order; f < s->ap_filter_count; f++) { \356 for (int f = s->first_order; f < s->ap_filter_count; f++) { \
346 const type *asrc = (const type *)frames[band]->extended_data[ch]; \357 const type *asrc = (const type *)frames[band]->extended_data[ch]; \
347 type *dst = (type *)frames[band]->extended_data[ch]; \358 type *dst = (type *)frames[band]->extended_data[ch]; \
348 BiquadContext *ap = &xover->ap[band][aband][f]; \359 BiquadContext *ap = &xover->ap[band][aband][f]; \
360 BiquadCoeffs *apc = &s->ap[aband][f]; \
349 \361 \
350 biquad_process_## name(ap, dst, asrc, nb_samples); \362 biquad_process_## name(apc, ap, dst, asrc, nb_samples); \
351 } \363 } \
352 } \364 } \
353 } \365 } \
@@ -385,29 +397,26 @@ static int config_input(AVFilterLink *inlink) · 31 unmodified lines
385 s->ap_filter_count = s->filter_count / 2 + s->first_order;397 s->ap_filter_count = s->filter_count / 2 + s->first_order;
386 calc_q_factors(s->order, q);398 calc_q_factors(s->order, q);
387 399
388 for (int ch = 0; ch < inlink->channels; ch++) {400 for (int band = 0; band <= s->nb_splits; band++) {
389 for (int band = 0; band <= s->nb_splits; band++) {401 if (s->first_order) {
390 if (s->first_order) {402 set_lp(&s->lp[band][0], s->splits[band], 0.5, sample_rate);
391 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);403 set_hp(&s->hp[band][0], s->splits[band], 0.5, sample_rate);
392 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);404 }
393 }
394 405
395 for (int n = s->first_order; n < s->filter_count; n++) {406 for (int n = s->first_order; n < s->filter_count; n++) {
396 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;407 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
397 408
398 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);409 set_lp(&s->lp[band][n], s->splits[band], q[idx], sample_rate);
399 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);410 set_hp(&s->hp[band][n], s->splits[band], q[idx], sample_rate);
400 }411 }
401 412
402 for (int x = 0; x <= s->nb_splits && s->first_order; x++)413 if (s->first_order)
403 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);414 set_ap1(&s->ap[band][0], s->splits[band], sample_rate);
404 415
405 for (int n = s->first_order; n < s->ap_filter_count; n++) {416 for (int n = s->first_order; n < s->ap_filter_count; n++) {
406 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);417 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
407 418
408 for (int x = 0; x <= s->nb_splits; x++)419 set_ap(&s->ap[band][n], s->splits[band], q[idx], sample_rate);
409 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
410 }
411 }420 }
412 }421 }
413 422

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