+#define BIQUAD_DII_FILTER(name, type, min, max, need_clipping) \
+static void biquad_dii_## name (BiquadsContext *s, \
+ const void *input, void *output, int len, \
+ double *z1, double *z2, \
+ double *unused1, double *unused2, \
+ double b0, double b1, double b2, \
+ double a1, double a2, int *clippings, \
+ int disabled) \
+{ \
+ const type *ibuf = input; \
+ type *obuf = output; \
+ double w1 = *z1; \
+ double w2 = *z2; \
+ double wet = s->mix; \
+ double dry = 1. - wet; \
+ double in, out, w0; \
+ \
+ a1 = -a1; \
+ a2 = -a2; \
+ \
+ for (int i = 0; i < len; i++) { \
+ in = ibuf[i]; \
+ w0 = in + a1 * w1 + a2 * w2; \
+ out = b0 * w0 + b1 * w1 + b2 * w2; \
+ w2 = w1; \
+ w1 = w0; \
+ out = out * wet + in * dry; \
+ if (disabled) { \
+ obuf[i] = in; \
+ } else if (need_clipping && out < min) { \
+ (*clippings)++; \
+ obuf[i] = min; \
+ } else if (need_clipping && out > max) { \
+ (*clippings)++; \
+ obuf[i] = max; \
+ } else { \
+ obuf[i] = out; \
+ } \
+ } \
+ *z1 = w1; \
+ *z2 = w2; \
+}
+
+BIQUAD_DII_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
+BIQUAD_DII_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
+BIQUAD_DII_FILTER(flt, float, -1., 1., 0)
+BIQUAD_DII_FILTER(dbl, double, -1., 1., 0)
+
+#define BIQUAD_TDII_FILTER(name, type, min, max, need_clipping) \
+static void biquad_tdii_## name (BiquadsContext *s, \
+ const void *input, void *output, int len, \
+ double *z1, double *z2, \
+ double *unused1, double *unused2, \
+ double b0, double b1, double b2, \
+ double a1, double a2, int *clippings, \
+ int disabled) \
+{ \
+ const type *ibuf = input; \
+ type *obuf = output; \
+ double w1 = *z1; \
+ double w2 = *z2; \
+ double wet = s->mix; \
+ double dry = 1. - wet; \
+ double in, out; \
+ \
+ a1 = -a1; \
+ a2 = -a2; \
+ \
+ for (int i = 0; i < len; i++) { \
+ in = ibuf[i]; \
+ out = b0 * in + w1; \
+ w1 = b1 * in + w2 + a1 * out; \
+ w2 = b2 * in + a2 * out; \
+ out = out * wet + in * dry; \
+ if (disabled) { \
+ obuf[i] = in; \
+ } else if (need_clipping && out < min) { \
+ (*clippings)++; \
+ obuf[i] = min; \
+ } else if (need_clipping && out > max) { \
+ (*clippings)++; \
+ obuf[i] = max; \
+ } else { \
+ obuf[i] = out; \
+ } \
+ } \
+ *z1 = w1; \
+ *z2 = w2; \
+}
+
+BIQUAD_TDII_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
+BIQUAD_TDII_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
+BIQUAD_TDII_FILTER(flt, float, -1., 1., 0)
+BIQUAD_TDII_FILTER(dbl, double, -1., 1., 0)
+
+#define BIQUAD_LATT_FILTER(name, type, min, max, need_clipping) \
+static void biquad_latt_## name (BiquadsContext *s, \
+ const void *input, void *output, int len, \
+ double *z1, double *z2, \
+ double *unused1, double *unused2, \
+ double v0, double v1, double v2, \
+ double k0, double k1, int *clippings, \
+ int disabled) \
+{ \
+ const type *ibuf = input; \
+ type *obuf = output; \
+ double s0 = *z1; \
+ double s1 = *z2; \
+ double wet = s->mix; \
+ double dry = 1. - wet; \
+ double in, out; \
+ double t0, t1; \
+ \
+ for (int i = 0; i < len; i++) { \
+ out = 0.; \
+ in = ibuf[i]; \
+ t0 = in - k1 * s0; \
+ t1 = t0 * k1 + s0; \
+ out += t1 * v2; \
+ \
+ t0 = t0 - k0 * s1; \
+ t1 = t0 * k0 + s1; \
+ out += t1 * v1; \
+ \
+ out += t0 * v0; \
+ s0 = t1; \
+ s1 = t0; \
+ \
+ out = out * wet + in * dry; \
+ if (disabled) { \
+ obuf[i] = in; \
+ } else if (need_clipping && out < min) { \
+ (*clippings)++; \
+ obuf[i] = min; \
+ } else if (need_clipping && out > max) { \
+ (*clippings)++; \
+ obuf[i] = max; \
+ } else { \
+ obuf[i] = out; \
+ } \
+ } \
+ *z1 = s0; \
+ *z2 = s1; \
+}
+
+BIQUAD_LATT_FILTER(s16, int16_t, INT16_MIN, INT16_MAX, 1)
+BIQUAD_LATT_FILTER(s32, int32_t, INT32_MIN, INT32_MAX, 1)
+BIQUAD_LATT_FILTER(flt, float, -1., 1., 0)
+BIQUAD_LATT_FILTER(dbl, double, -1., 1., 0)
+
+static void convert_dir2latt(BiquadsContext *s)
+{
+ double k0, k1, v0, v1, v2;
+
+ k1 = s->a2;
+ k0 = s->a1 / (1. + k1);
+ v2 = s->b2;
+ v1 = s->b1 - v2 * s->a1;
+ v0 = s->b0 - v1 * k0 - v2 * k1;
+
+ s->a1 = k0;
+ s->a2 = k1;
+ s->b0 = v0;
+ s->b1 = v1;
+ s->b2 = v2;
+}
+