History

avfilter/af_acrossover: improve quality for odd order butterworth

Paul B Mahol committed 5 years ago

Commit 1555cfe1 parent 38648961 file · +37 −8Browse files

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

Unified · Split

libavfilter/af_acrossover.c+37−8
@@ -58,6 +58,8 @@ typedef struct AudioCrossoverContext { · 57 unmodified lines
58 58
59 int order;59 int order;
60 int filter_count;60 int filter_count;
61 int first_order;
62 int ap_filter_count;
61 int nb_splits;63 int nb_splits;
62 float *splits;64 float *splits;
63 65
@@ -205,6 +207,18 @@ static void set_ap(BiquadContext *b, double fc, double q, double sr) · 141 unmodified lines
205 b->a2 = -a2 / a0;207 b->a2 = -a2 / a0;
206}208}
207 209
210static void set_ap1(BiquadContext *b, double fc, double sr)
211{
212 double omega = M_PI * fc / sr;
213 double K = tan(omega);
214
215 b->a1 = -(1. - K) / (1. + K);
216 b->a2 = 0.;
217 b->b0 = -b->a1;
218 b->b1 = 1.;
219 b->b2 = 0.;
220}
221
208static void calc_q_factors(int order, double *q)222static void calc_q_factors(int order, double *q)
209{223{
210 double n = order / 2.;224 double n = order / 2.;
@@ -218,7 +232,6 @@ static int config_input(AVFilterLink *inlink) · 7 unmodified lines
218 AVFilterContext *ctx = inlink->dst;232 AVFilterContext *ctx = inlink->dst;
219 AudioCrossoverContext *s = ctx->priv;233 AudioCrossoverContext *s = ctx->priv;
220 int sample_rate = inlink->sample_rate;234 int sample_rate = inlink->sample_rate;
221 int first_order;
222 double q[16];235 double q[16];
223 236
224 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));237 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
@@ -227,21 +240,29 @@ static int config_input(AVFilterLink *inlink) · 2 unmodified lines
227 240
228 s->order = (s->order_opt + 1) * 2;241 s->order = (s->order_opt + 1) * 2;
229 s->filter_count = s->order / 2;242 s->filter_count = s->order / 2;
230 first_order = s->filter_count & 1;243 s->first_order = s->filter_count & 1;
244 s->ap_filter_count = s->filter_count / 2 + s->first_order;
231 calc_q_factors(s->order, q);245 calc_q_factors(s->order, q);
232 246
233 for (int ch = 0; ch < inlink->channels; ch++) {247 for (int ch = 0; ch < inlink->channels; ch++) {
234 for (int band = 0; band <= s->nb_splits; band++) {248 for (int band = 0; band <= s->nb_splits; band++) {
235 if (first_order) {249 if (s->first_order) {
236 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);250 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
237 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);251 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
238 }252 }
239 253
240 for (int n = first_order; n < s->filter_count; n++) {254 for (int n = s->first_order; n < s->filter_count; n++) {
241 const int idx = s->filter_count / 2 - ((n + first_order) / 2 - first_order) - 1;255 const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
242 256
243 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);257 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
244 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);258 set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate);
259 }
260
261 for (int x = 0; x <= s->nb_splits && s->first_order; x++)
262 set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate);
263
264 for (int n = s->first_order; n < s->ap_filter_count; n++) {
265 const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1);
245 266
246 for (int x = 0; x <= s->nb_splits; x++)267 for (int x = 0; x <= s->nb_splits; x++)
247 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);268 set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate);
@@ -340,17 +361,25 @@ static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 92 unmodified lines
340 }361 }
341 362
342 for (int aband = band + 1; aband < ctx->nb_outputs; aband++) {363 for (int aband = band + 1; aband < ctx->nb_outputs; aband++) {
343 for (int f = 0; f < s->filter_count / 2; f++) {364 if (s->first_order) {
365 const double *src = (const double *)frames[band]->extended_data[ch];
366 double *dst = (double *)frames[band]->extended_data[ch];
367 BiquadContext *ap = &xover->ap[band][aband][0];
368
369 biquad_process(ap, dst, src, nb_samples);
370 }
371
372 for (int f = s->first_order; f < s->ap_filter_count; f++) {
344 const double *src = (const double *)frames[band]->extended_data[ch];373 const double *src = (const double *)frames[band]->extended_data[ch];
345 double *dst = (double *)frames[band]->extended_data[ch];374 double *dst = (double *)frames[band]->extended_data[ch];
346 BiquadContext *ap = &xover->ap[band][aband][f * 2 + (s->filter_count & 1)];375 BiquadContext *ap = &xover->ap[band][aband][f];
347 376
348 biquad_process(ap, dst, src, nb_samples);377 biquad_process(ap, dst, src, nb_samples);
349 }378 }
350 }379 }
351 }380 }
352 381
353 for (int band = 0; band < ctx->nb_outputs && (s->filter_count & 1); band++) {382 for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) {
354 if (band & 1) {383 if (band & 1) {
355 double *dst = (double *)frames[band]->extended_data[ch];384 double *dst = (double *)frames[band]->extended_data[ch];
356 385

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