History

avfilter/af_aiir: implement parallel processing

Paul B Mahol committed 6 years ago

Commit 0df0e121 parent 0ed455b2 files · +228 −18Browse files

Unified · Split

2 files
  1. doc/filters.texi +10 −2
  2. libavfilter/af_aiir.c +218 −16
doc/filters.texi+10−2
@@ -1421,8 +1421,16 @@ S-plane zeros/poles · 1420 unmodified lines
14211421 @end table
14221422
14231423 @item process, r
1424−Set kind of processing.
1425−Can be @code{d} - direct or @code{s} - serial cascading. Default is @code{s}.
1424+Set type of processing.
1425+
1426+@table @samp
1427+@item d
1428+direct processing
1429+@item s
1430+serial processing
1431+@item p
1432+parallel processing
1433+@end table
14261434
14271435 @item precision, e
14281436 Set filtering precision.
libavfilter/af_aiir.c+218−16
@@ -49,6 +49,7 @@ typedef struct IIRChannel { · 48 unmodified lines
4949 double *ab[2];
5050 double g;
5151 double *cache[2];
52+ double fir;
5253 BiquadContext *biquads;
5354 int clippings;
5455 } IIRChannel;
@@ -183,6 +184,7 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb · 128 unmodified lines
183184 const double ig = s->dry_gain; \
184185 const double og = s->wet_gain; \
185186 const double mix = s->mix; \
187+ const double imix = 1. - mix; \
186188 ThreadData *td = arg; \
187189 AVFrame *in = td->in, *out = td->out; \
188190 const type *src = (const type *)in->extended_data[ch]; \
@@ -205,16 +207,16 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb · 16 unmodified lines
205207 double o2 = iir->biquads[i].o2; \
206208 \
207209 for (n = 0; n < in->nb_samples; n++) { \
208− double sample = ig * (i ? dst[n] : src[n]); \
209− double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
210+ double i0 = ig * (i ? dst[n] : src[n]); \
211+ double o0 = i0 * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
210212 \
211213 i2 = i1; \
212− i1 = src[n]; \
214+ i1 = i0; \
213215 o2 = o1; \
214216 o1 = o0; \
215217 o0 *= og * g; \
216218 \
217− o0 = o0 * mix + (1. - mix) * sample; \
219+ o0 = o0 * mix + imix * i0; \
218220 if (need_clipping && o0 < min) { \
219221 (*clippings)++; \
220222 dst[n] = min; \
@@ -239,6 +241,76 @@ SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1) · 18 unmodified lines
239241 SERIAL_IIR_CH(fltp, float, -1., 1., 0)
240242 SERIAL_IIR_CH(dblp, double, -1., 1., 0)
241243
244+#define PARALLEL_IIR_CH(name, type, min, max, need_clipping) \
245+static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg, \
246+ int ch, int nb_jobs) \
247+{ \
248+ AudioIIRContext *s = ctx->priv; \
249+ const double ig = s->dry_gain; \
250+ const double og = s->wet_gain; \
251+ const double mix = s->mix; \
252+ const double imix = 1. - mix; \
253+ ThreadData *td = arg; \
254+ AVFrame *in = td->in, *out = td->out; \
255+ const type *src = (const type *)in->extended_data[ch]; \
256+ type *dst = (type *)out->extended_data[ch]; \
257+ IIRChannel *iir = &s->iir[ch]; \
258+ const double g = iir->g; \
259+ const double fir = iir->fir; \
260+ int *clippings = &iir->clippings; \
261+ int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \
262+ int n, i; \
263+ \
264+ for (i = 0; i < nb_biquads; i++) { \
265+ const double a1 = -iir->biquads[i].a[1]; \
266+ const double a2 = -iir->biquads[i].a[2]; \
267+ const double b1 = iir->biquads[i].b[1]; \
268+ const double b2 = iir->biquads[i].b[2]; \
269+ double i1 = iir->biquads[i].i1; \
270+ double i2 = iir->biquads[i].i2; \
271+ double o1 = iir->biquads[i].o1; \
272+ double o2 = iir->biquads[i].o2; \
273+ \
274+ for (n = 0; n < in->nb_samples; n++) { \
275+ double i0 = ig * src[n]; \
276+ double o0 = i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \
277+ \
278+ i2 = i1; \
279+ i1 = i0; \
280+ o2 = o1; \
281+ o1 = o0; \
282+ o0 *= og * g; \
283+ o0 += dst[n]; \
284+ \
285+ if (need_clipping && o0 < min) { \
286+ (*clippings)++; \
287+ dst[n] = min; \
288+ } else if (need_clipping && o0 > max) { \
289+ (*clippings)++; \
290+ dst[n] = max; \
291+ } else { \
292+ dst[n] = o0; \
293+ } \
294+ } \
295+ iir->biquads[i].i1 = i1; \
296+ iir->biquads[i].i2 = i2; \
297+ iir->biquads[i].o1 = o1; \
298+ iir->biquads[i].o2 = o2; \
299+ } \
300+ \
301+ for (n = 0; n < in->nb_samples; n++) { \
302+ dst[n] += fir * src[n]; \
303+ dst[n] = dst[n] * mix + imix * src[n]; \
304+ } \
305+ \
306+ return 0; \
307+}
308+
309+PARALLEL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1)
310+PARALLEL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1)
311+PARALLEL_IIR_CH(fltp, float, -1., 1., 0)
312+PARALLEL_IIR_CH(dblp, double, -1., 1., 0)
313+
242314 static void count_coefficients(char *item_str, int *nb_items)
243315 {
244316 char *p;
@@ -656,6 +728,128 @@ static int decompose_zp2biquads(AVFilterContext *ctx, int channels) · 411 unmodified lines
656728 return 0;
657729 }
658730
731+static void biquad_process(double *x, double *y, int length,
732+ double b0, double b1, double b2,
733+ double a1, double a2)
734+{
735+ double w1 = 0., w2 = 0.;
736+
737+ a1 = -a1;
738+ a2 = -a2;
739+
740+ for (int n = 0; n < length; n++) {
741+ double out, in = x[n];
742+
743+ y[n] = out = in * b0 + w1;
744+ w1 = b1 * in + w2 + a1 * out;
745+ w2 = b2 * in + a2 * out;
746+ }
747+}
748+
749+static void solve(double *matrix, double *vector, int n, double *y, double *x, double *lu)
750+{
751+ double sum = 0.;
752+
753+ for (int i = 0; i < n; i++) {
754+ for (int j = i; j < n; j++) {
755+ sum = 0.;
756+ for (int k = 0; k < i; k++)
757+ sum += lu[i * n + k] * lu[k * n + j];
758+ lu[i * n + j] = matrix[j * n + i] - sum;
759+ }
760+ for (int j = i + 1; j < n; j++) {
761+ sum = 0.;
762+ for (int k = 0; k < i; k++)
763+ sum += lu[j * n + k] * lu[k * n + i];
764+ lu[j * n + i] = (1. / lu[i * n + i]) * (matrix[i * n + j] - sum);
765+ }
766+ }
767+
768+ for (int i = 0; i < n; i++) {
769+ sum = 0.;
770+ for (int k = 0; k < i; k++)
771+ sum += lu[i * n + k] * y[k];
772+ y[i] = vector[i] - sum;
773+ }
774+
775+ for (int i = n - 1; i >= 0; i--) {
776+ sum = 0.;
777+ for (int k = i + 1; k < n; k++)
778+ sum += lu[i * n + k] * x[k];
779+ x[i] = (1 / lu[i * n + i]) * (y[i] - sum);
780+ }
781+}
782+
783+static int convert_serial2parallel(AVFilterContext *ctx, int channels)
784+{
785+ AudioIIRContext *s = ctx->priv;
786+ int ret = 0;
787+
788+ for (int ch = 0; ch < channels; ch++) {
789+ IIRChannel *iir = &s->iir[ch];
790+ int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2;
791+ int length = nb_biquads * 2 + 1;
792+ double *impulse = av_calloc(length, sizeof(*impulse));
793+ double *y = av_calloc(length, sizeof(*y));
794+ double *resp = av_calloc(length, sizeof(*resp));
795+ double *M = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*M));
796+ double *W = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*W));
797+
798+ if (!impulse || !y || !resp || !M) {
799+ av_free(impulse);
800+ av_free(y);
801+ av_free(resp);
802+ av_free(M);
803+ av_free(W);
804+ return AVERROR(ENOMEM);
805+ }
806+
807+ impulse[0] = 1.;
808+
809+ for (int n = 0; n < nb_biquads; n++) {
810+ BiquadContext *biquad = &iir->biquads[n];
811+
812+ biquad_process(n ? y : impulse, y, length,
813+ biquad->b[0], biquad->b[1], biquad->b[2],
814+ biquad->a[1], biquad->a[2]);
815+ }
816+
817+ for (int n = 0; n < nb_biquads; n++) {
818+ BiquadContext *biquad = &iir->biquads[n];
819+
820+ biquad_process(impulse, resp, length - 1,
821+ 1., 0., 0., biquad->a[1], biquad->a[2]);
822+
823+ memcpy(M + n * 2 * (length - 1), resp, sizeof(*resp) * (length - 1));
824+ memcpy(M + n * 2 * (length - 1) + length, resp, sizeof(*resp) * (length - 2));
825+ memset(resp, 0, length * sizeof(*resp));
826+ }
827+
828+ solve(M, &y[1], length - 1, &impulse[1], resp, W);
829+
830+ iir->fir = y[0];
831+
832+ for (int n = 0; n < nb_biquads; n++) {
833+ BiquadContext *biquad = &iir->biquads[n];
834+
835+ biquad->b[0] = 0.;
836+ biquad->b[1] = resp[n * 2 + 0];
837+ biquad->b[2] = resp[n * 2 + 1];
838+ }
839+
840+ av_free(impulse);
841+ av_free(y);
842+ av_free(resp);
843+ av_free(M);
844+ av_free(W);
845+
846+ if (ret < 0)
847+ return ret;
848+ }
849+
850+ return 0;
851+}
852+
659853 static void convert_pr2zp(AVFilterContext *ctx, int channels)
660854 {
661855 AudioIIRContext *s = ctx->priv;
@@ -1034,15 +1228,22 @@ static int config_output(AVFilterLink *outlink) · 372 unmodified lines
10341228 if (ret < 0)
10351229 return ret;
10361230 } else if (s->format == 0 && s->process == 1) {
1037− av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n");
1231+ av_log(ctx, AV_LOG_ERROR, "Serial processing is not implemented for transfer function.\n");
1232+ return AVERROR_PATCHWELCOME;
1233+ } else if (s->format == 0 && s->process == 2) {
1234+ av_log(ctx, AV_LOG_ERROR, "Parallel processing is not implemented for transfer function.\n");
10381235 return AVERROR_PATCHWELCOME;
10391236 } else if (s->format > 0 && s->process == 1) {
1040− if (inlink->format == AV_SAMPLE_FMT_S16P)
1041− av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n");
1042−
10431237 ret = decompose_zp2biquads(ctx, inlink->channels);
10441238 if (ret < 0)
10451239 return ret;
1240+ } else if (s->format > 0 && s->process == 2) {
1241+ ret = decompose_zp2biquads(ctx, inlink->channels);
1242+ if (ret < 0)
1243+ return ret;
1244+ ret = convert_serial2parallel(ctx, inlink->channels);
1245+ if (ret < 0)
1246+ return ret;
10461247 }
10471248
10481249 for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) {
@@ -1061,10 +1262,10 @@ static int config_output(AVFilterLink *outlink) · 12 unmodified lines
10611262 }
10621263
10631264 switch (inlink->format) {
1064− case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
1065− case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
1066− case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
1067− case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
1265+ case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 2 ? iir_ch_parallel_dblp : s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break;
1266+ case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 2 ? iir_ch_parallel_fltp : s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break;
1267+ case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s32p : s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break;
1268+ case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s16p : s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break;
10681269 }
10691270
10701271 return 0;
@@ -1079,7 +1280,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 8 unmodified lines
10791280 AVFrame *out;
10801281 int ch, ret;
10811282
1082− if (av_frame_is_writable(in)) {
1283+ if (av_frame_is_writable(in) && s->process != 2) {
10831284 out = in;
10841285 } else {
10851286 out = ff_get_audio_buffer(outlink, in->nb_samples);
@@ -1232,10 +1433,11 @@ static const AVOption aiir_options[] = { · 146 unmodified lines
12321433 { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" },
12331434 { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" },
12341435 { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" },
1235− { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
1236− { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" },
1436+ { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" },
1437+ { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" },
12371438 { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" },
1238− { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
1439+ { "s", "serial", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" },
1440+ { "p", "parallel", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "process" },
12391441 { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
12401442 { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" },
12411443 { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" },

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