+ return 0;
+}
+
+static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
+ enum IIRFilterMode filt_mode, int order,
+ float cutoff_ratio, float stopband)
+{
+ double cos_w0, sin_w0;
+ double a0, x0, x1;
+
+ if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
+ filt_mode != FF_FILTER_MODE_LOWPASS) {
+ av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
+ "high-pass and low-pass filter modes\n");
+ return -1;
+ }
+ if (order != 2) {
+ av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
+ return -1;
+ }
+
+ cos_w0 = cos(M_PI * cutoff_ratio);
+ sin_w0 = sin(M_PI * cutoff_ratio);
+
+ a0 = 1.0 + (sin_w0 / 2.0);
+
+ if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
+ c->gain = ((1.0 + cos_w0) / 2.0) / a0;
+ x0 = ((1.0 + cos_w0) / 2.0) / a0;
+ x1 = (-(1.0 + cos_w0)) / a0;
+ } else { // FF_FILTER_MODE_LOWPASS
+ c->gain = ((1.0 - cos_w0) / 2.0) / a0;
+ x0 = ((1.0 - cos_w0) / 2.0) / a0;
+ x1 = (1.0 - cos_w0) / a0;
+ }
+ c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
+ c->cy[1] = (2.0 * cos_w0) / a0;
+
+ // divide by gain to make the x coeffs integers.
+ // during filtering, the delay state will include the gain multiplication
+ c->cx[0] = lrintf(x0 / c->gain);
+ c->cx[1] = lrintf(x1 / c->gain);
+
+ return 0;
+}
+
+av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
+ enum IIRFilterType filt_type,
+ enum IIRFilterMode filt_mode,
+ int order, float cutoff_ratio,
+ float stopband, float ripple)
+{
+ FFIIRFilterCoeffs *c;
+ int ret = 0;
+
+ if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
+ return NULL;
+
+ FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
+ init_fail);
+ FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
+ init_fail);
+ FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
+ init_fail);
+ c->order = order;
+
+ switch (filt_type) {
+ case FF_FILTER_TYPE_BUTTERWORTH:
+ ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
+ stopband);
+ break;
+ case FF_FILTER_TYPE_BIQUAD:
+ ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
+ stopband);
+ break;
+ default:
+ av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
+ goto init_fail;
+ }
+
+ if (!ret)
+ return c;
+
+init_fail:
+ ff_iir_filter_free_coeffs(c);
+ return NULL;