You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
11KB

  1. /*
  2. * IIR filter
  3. * Copyright (c) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * different IIR filters implementation
  24. */
  25. #include <math.h>
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/common.h"
  28. #include "iirfilter.h"
  29. /**
  30. * IIR filter global parameters
  31. */
  32. typedef struct FFIIRFilterCoeffs {
  33. int order;
  34. float gain;
  35. int *cx;
  36. float *cy;
  37. } FFIIRFilterCoeffs;
  38. /**
  39. * IIR filter state
  40. */
  41. typedef struct FFIIRFilterState {
  42. float x[1];
  43. } FFIIRFilterState;
  44. /// maximum supported filter order
  45. #define MAXORDER 30
  46. static av_cold int butterworth_init_coeffs(void *avc,
  47. struct FFIIRFilterCoeffs *c,
  48. enum IIRFilterMode filt_mode,
  49. int order, float cutoff_ratio,
  50. float stopband)
  51. {
  52. int i, j;
  53. double wa;
  54. double p[MAXORDER + 1][2];
  55. if (filt_mode != FF_FILTER_MODE_LOWPASS) {
  56. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  57. "low-pass filter mode\n");
  58. return -1;
  59. }
  60. if (order & 1) {
  61. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  62. "even filter orders\n");
  63. return -1;
  64. }
  65. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  66. c->cx[0] = 1;
  67. for (i = 1; i < (order >> 1) + 1; i++)
  68. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  69. p[0][0] = 1.0;
  70. p[0][1] = 0.0;
  71. for (i = 1; i <= order; i++)
  72. p[i][0] = p[i][1] = 0.0;
  73. for (i = 0; i < order; i++) {
  74. double zp[2];
  75. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  76. double a_re, a_im, c_re, c_im;
  77. zp[0] = cos(th) * wa;
  78. zp[1] = sin(th) * wa;
  79. a_re = zp[0] + 2.0;
  80. c_re = zp[0] - 2.0;
  81. a_im =
  82. c_im = zp[1];
  83. zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
  84. zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
  85. for (j = order; j >= 1; j--) {
  86. a_re = p[j][0];
  87. a_im = p[j][1];
  88. p[j][0] = a_re * zp[0] - a_im * zp[1] + p[j - 1][0];
  89. p[j][1] = a_re * zp[1] + a_im * zp[0] + p[j - 1][1];
  90. }
  91. a_re = p[0][0] * zp[0] - p[0][1] * zp[1];
  92. p[0][1] = p[0][0] * zp[1] + p[0][1] * zp[0];
  93. p[0][0] = a_re;
  94. }
  95. c->gain = p[order][0];
  96. for (i = 0; i < order; i++) {
  97. c->gain += p[i][0];
  98. c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
  99. (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
  100. }
  101. c->gain /= 1 << order;
  102. return 0;
  103. }
  104. static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
  105. enum IIRFilterMode filt_mode, int order,
  106. float cutoff_ratio, float stopband)
  107. {
  108. double cos_w0, sin_w0;
  109. double a0, x0, x1;
  110. if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
  111. filt_mode != FF_FILTER_MODE_LOWPASS) {
  112. av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
  113. "high-pass and low-pass filter modes\n");
  114. return -1;
  115. }
  116. if (order != 2) {
  117. av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
  118. return -1;
  119. }
  120. cos_w0 = cos(M_PI * cutoff_ratio);
  121. sin_w0 = sin(M_PI * cutoff_ratio);
  122. a0 = 1.0 + (sin_w0 / 2.0);
  123. if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
  124. c->gain = ((1.0 + cos_w0) / 2.0) / a0;
  125. x0 = ((1.0 + cos_w0) / 2.0) / a0;
  126. x1 = (-(1.0 + cos_w0)) / a0;
  127. } else { // FF_FILTER_MODE_LOWPASS
  128. c->gain = ((1.0 - cos_w0) / 2.0) / a0;
  129. x0 = ((1.0 - cos_w0) / 2.0) / a0;
  130. x1 = (1.0 - cos_w0) / a0;
  131. }
  132. c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
  133. c->cy[1] = (2.0 * cos_w0) / a0;
  134. // divide by gain to make the x coeffs integers.
  135. // during filtering, the delay state will include the gain multiplication
  136. c->cx[0] = lrintf(x0 / c->gain);
  137. c->cx[1] = lrintf(x1 / c->gain);
  138. return 0;
  139. }
  140. av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
  141. enum IIRFilterType filt_type,
  142. enum IIRFilterMode filt_mode,
  143. int order, float cutoff_ratio,
  144. float stopband, float ripple)
  145. {
  146. FFIIRFilterCoeffs *c;
  147. int ret = 0;
  148. if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
  149. return NULL;
  150. FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
  151. init_fail);
  152. FF_ALLOC_OR_GOTO(avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
  153. init_fail);
  154. FF_ALLOC_OR_GOTO(avc, c->cy, sizeof(c->cy[0]) * order,
  155. init_fail);
  156. c->order = order;
  157. switch (filt_type) {
  158. case FF_FILTER_TYPE_BUTTERWORTH:
  159. ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  160. stopband);
  161. break;
  162. case FF_FILTER_TYPE_BIQUAD:
  163. ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  164. stopband);
  165. break;
  166. default:
  167. av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
  168. goto init_fail;
  169. }
  170. if (!ret)
  171. return c;
  172. init_fail:
  173. ff_iir_filter_free_coeffs(c);
  174. return NULL;
  175. }
  176. av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
  177. {
  178. FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  179. return s;
  180. }
  181. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  182. #define CONV_FLT(dest, source) dest = source;
  183. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  184. in = *src0 * c->gain + \
  185. c->cy[0] * s->x[i0] + \
  186. c->cy[1] * s->x[i1] + \
  187. c->cy[2] * s->x[i2] + \
  188. c->cy[3] * s->x[i3]; \
  189. res = (s->x[i0] + in) * 1 + \
  190. (s->x[i1] + s->x[i3]) * 4 + \
  191. s->x[i2] * 6; \
  192. CONV_ ## fmt(*dst0, res) \
  193. s->x[i0] = in; \
  194. src0 += sstep; \
  195. dst0 += dstep;
  196. #define FILTER_BW_O4(type, fmt) { \
  197. int i; \
  198. const type *src0 = src; \
  199. type *dst0 = dst; \
  200. for (i = 0; i < size; i += 4) { \
  201. float in, res; \
  202. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  203. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  204. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  205. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  206. } \
  207. }
  208. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  209. int i; \
  210. const type *src0 = src; \
  211. type *dst0 = dst; \
  212. for (i = 0; i < size; i++) { \
  213. int j; \
  214. float in, res; \
  215. in = *src0 * c->gain; \
  216. for (j = 0; j < c->order; j++) \
  217. in += c->cy[j] * s->x[j]; \
  218. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  219. for (j = 1; j < c->order >> 1; j++) \
  220. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  221. for (j = 0; j < c->order - 1; j++) \
  222. s->x[j] = s->x[j + 1]; \
  223. CONV_ ## fmt(*dst0, res) \
  224. s->x[c->order - 1] = in; \
  225. src0 += sstep; \
  226. dst0 += dstep; \
  227. } \
  228. }
  229. #define FILTER_O2(type, fmt) { \
  230. int i; \
  231. const type *src0 = src; \
  232. type *dst0 = dst; \
  233. for (i = 0; i < size; i++) { \
  234. float in = *src0 * c->gain + \
  235. s->x[0] * c->cy[0] + \
  236. s->x[1] * c->cy[1]; \
  237. CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
  238. s->x[0] = s->x[1]; \
  239. s->x[1] = in; \
  240. src0 += sstep; \
  241. dst0 += dstep; \
  242. } \
  243. }
  244. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  245. struct FFIIRFilterState *s, int size,
  246. const int16_t *src, int sstep, int16_t *dst, int dstep)
  247. {
  248. if (c->order == 2) {
  249. FILTER_O2(int16_t, S16)
  250. } else if (c->order == 4) {
  251. FILTER_BW_O4(int16_t, S16)
  252. } else {
  253. FILTER_DIRECT_FORM_II(int16_t, S16)
  254. }
  255. }
  256. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  257. struct FFIIRFilterState *s, int size,
  258. const float *src, int sstep, float *dst, int dstep)
  259. {
  260. if (c->order == 2) {
  261. FILTER_O2(float, FLT)
  262. } else if (c->order == 4) {
  263. FILTER_BW_O4(float, FLT)
  264. } else {
  265. FILTER_DIRECT_FORM_II(float, FLT)
  266. }
  267. }
  268. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  269. {
  270. av_free(state);
  271. }
  272. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  273. {
  274. if (coeffs) {
  275. av_free(coeffs->cx);
  276. av_free(coeffs->cy);
  277. }
  278. av_free(coeffs);
  279. }