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
5858
5959 int order;
6060 int filter_count;
61+ int first_order;
62+ int ap_filter_count;
6163 int nb_splits;
6264 float *splits;
6365
@@ -205,6 +207,18 @@ static void set_ap(BiquadContext *b, double fc, double q, double sr) · 141 unmodified lines
205207 b->a2 = -a2 / a0;
206208 }
207209
210+static 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+
208222 static void calc_q_factors(int order, double *q)
209223 {
210224 double n = order / 2.;
@@ -218,7 +232,6 @@ static int config_input(AVFilterLink *inlink) · 7 unmodified lines
218232 AVFilterContext *ctx = inlink->dst;
219233 AudioCrossoverContext *s = ctx->priv;
220234 int sample_rate = inlink->sample_rate;
221− int first_order;
222235 double q[16];
223236
224237 s->xover = av_calloc(inlink->channels, sizeof(*s->xover));
@@ -227,21 +240,29 @@ static int config_input(AVFilterLink *inlink) · 2 unmodified lines
227240
228241 s->order = (s->order_opt + 1) * 2;
229242 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;
231245 calc_q_factors(s->order, q);
232246
233247 for (int ch = 0; ch < inlink->channels; ch++) {
234248 for (int band = 0; band <= s->nb_splits; band++) {
235− if (first_order) {
249+ if (s->first_order) {
236250 set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate);
237251 set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate);
238252 }
239253
240− for (int n = first_order; n < s->filter_count; n++) {
241− const int idx = s->filter_count / 2 - ((n + first_order) / 2 - first_order) - 1;
254+ for (int n = s->first_order; n < s->filter_count; n++) {
255+ const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1;
242256
243257 set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate);
244258 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);
245266
246267 for (int x = 0; x <= s->nb_splits; x++)
247268 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
340361 }
341362
342363 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++) {
344373 const double *src = (const double *)frames[band]->extended_data[ch];
345374 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];
347376
348377 biquad_process(ap, dst, src, nb_samples);
349378 }
350379 }
351380 }
352381
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++) {
354383 if (band & 1) {
355384 double *dst = (double *)frames[band]->extended_data[ch];
356385

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