/// maximum supported filter order
#define MAXORDER 30
-av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
- enum IIRFilterMode filt_mode,
- int order, float cutoff_ratio,
- float stopband, float ripple)
+static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
+ enum IIRFilterMode filt_mode,
+ int order, float cutoff_ratio,
+ float stopband)
{
int i, j;
- FFIIRFilterCoeffs *c;
double wa;
double p[MAXORDER + 1][2];
- if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
- return NULL;
- if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
- return NULL;
-
- c = av_malloc(sizeof(FFIIRFilterCoeffs));
- c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
- c->cy = av_malloc(sizeof(c->cy[0]) * order);
- c->order = order;
+ if (filt_mode != FF_FILTER_MODE_LOWPASS) {
+ av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
+ "low-pass filter mode\n");
+ return -1;
+ }
+ if (order & 1) {
+ av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
+ "even filter orders\n");
+ return -1;
+ }
wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
}
c->gain /= 1 << order;
- return c;
+ return 0;
+}
+
+static 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);
+ c->cy[0] /= c->gain;
+ c->cy[1] /= 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;
}
av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
return s;
}
-#define FILTER(i0, i1, i2, i3) \
- in = *src * c->gain \
- + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
- + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
- res = (s->x[i0] + in )*1 \
- + (s->x[i1] + s->x[i3])*4 \
- + s->x[i2] *6; \
- *dst = av_clip_int16(lrintf(res)); \
- s->x[i0] = in; \
- src += sstep; \
- dst += dstep; \
-
-void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
-{
- int i;
+#define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
- if(c->order == 4){
- for(i = 0; i < size; i += 4){
- float in, res;
+#define CONV_FLT(dest, source) dest = source;
- FILTER(0, 1, 2, 3);
- FILTER(1, 2, 3, 0);
- FILTER(2, 3, 0, 1);
- FILTER(3, 0, 1, 2);
- }
- }else{
- for(i = 0; i < size; i++){
- int j;
- float in, res;
- in = *src * c->gain;
- for(j = 0; j < c->order; j++)
- in += c->cy[j] * s->x[j];
- res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
- for(j = 1; j < c->order >> 1; j++)
- res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
- for(j = 0; j < c->order - 1; j++)
- s->x[j] = s->x[j + 1];
- *dst = av_clip_int16(lrintf(res));
- s->x[c->order - 1] = in;
- src += sstep;
- dst += dstep;
- }
+#define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
+ in = *src0 * c->gain \
+ + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
+ + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
+ res = (s->x[i0] + in )*1 \
+ + (s->x[i1] + s->x[i3])*4 \
+ + s->x[i2] *6; \
+ CONV_##fmt(*dst0, res) \
+ s->x[i0] = in; \
+ src0 += sstep; \
+ dst0 += dstep;
+
+#define FILTER_BW_O4(type, fmt) { \
+ int i; \
+ const type *src0 = src; \
+ type *dst0 = dst; \
+ for (i = 0; i < size; i += 4) { \
+ float in, res; \
+ FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
+ FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
+ FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
+ FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
+ } \
+}
+
+#define FILTER_DIRECT_FORM_II(type, fmt) { \
+ int i; \
+ const type *src0 = src; \
+ type *dst0 = dst; \
+ for (i = 0; i < size; i++) { \
+ int j; \
+ float in, res; \
+ in = *src0 * c->gain; \
+ for(j = 0; j < c->order; j++) \
+ in += c->cy[j] * s->x[j]; \
+ res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
+ for(j = 1; j < c->order >> 1; j++) \
+ res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
+ for(j = 0; j < c->order - 1; j++) \
+ s->x[j] = s->x[j + 1]; \
+ CONV_##fmt(*dst0, res) \
+ s->x[c->order - 1] = in; \
+ src0 += sstep; \
+ dst0 += dstep; \
+ } \
+}
+
+#define FILTER_O2(type, fmt) { \
+ int i; \
+ const type *src0 = src; \
+ type *dst0 = dst; \
+ for (i = 0; i < size; i++) { \
+ float in = *src0 * c->gain + \
+ s->x[0] * c->cy[0] + \
+ s->x[1] * c->cy[1]; \
+ CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
+ s->x[0] = s->x[1]; \
+ s->x[1] = in; \
+ src0 += sstep; \
+ dst0 += dstep; \
+ } \
+}
+
+void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
+ struct FFIIRFilterState *s, int size,
+ const int16_t *src, int sstep, int16_t *dst, int dstep)
+{
+ if (c->order == 2) {
+ FILTER_O2(int16_t, S16)
+ } else if (c->order == 4) {
+ FILTER_BW_O4(int16_t, S16)
+ } else {
+ FILTER_DIRECT_FORM_II(int16_t, S16)
+ }
+}
+
+void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
+ struct FFIIRFilterState *s, int size,
+ const float *src, int sstep, float *dst, int dstep)
+{
+ if (c->order == 2) {
+ FILTER_O2(float, FLT)
+ } else if (c->order == 4) {
+ FILTER_BW_O4(float, FLT)
+ } else {
+ FILTER_DIRECT_FORM_II(float, FLT)
}
}