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.

321 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 "libavutil/log.h"
  29. #include "iirfilter.h"
  30. /**
  31. * IIR filter global parameters
  32. */
  33. typedef struct FFIIRFilterCoeffs {
  34. int order;
  35. float gain;
  36. int *cx;
  37. float *cy;
  38. } FFIIRFilterCoeffs;
  39. /**
  40. * IIR filter state
  41. */
  42. typedef struct FFIIRFilterState {
  43. float x[1];
  44. } FFIIRFilterState;
  45. /// maximum supported filter order
  46. #define MAXORDER 30
  47. static av_cold int butterworth_init_coeffs(void *avc,
  48. struct FFIIRFilterCoeffs *c,
  49. enum IIRFilterMode filt_mode,
  50. int order, float cutoff_ratio,
  51. float stopband)
  52. {
  53. int i, j;
  54. double wa;
  55. double p[MAXORDER + 1][2];
  56. if (filt_mode != FF_FILTER_MODE_LOWPASS) {
  57. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  58. "low-pass filter mode\n");
  59. return -1;
  60. }
  61. if (order & 1) {
  62. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  63. "even filter orders\n");
  64. return -1;
  65. }
  66. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  67. c->cx[0] = 1;
  68. for (i = 1; i < (order >> 1) + 1; i++)
  69. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  70. p[0][0] = 1.0;
  71. p[0][1] = 0.0;
  72. for (i = 1; i <= order; i++)
  73. p[i][0] = p[i][1] = 0.0;
  74. for (i = 0; i < order; i++) {
  75. double zp[2];
  76. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  77. double a_re, a_im, c_re, c_im;
  78. zp[0] = cos(th) * wa;
  79. zp[1] = sin(th) * wa;
  80. a_re = zp[0] + 2.0;
  81. c_re = zp[0] - 2.0;
  82. a_im =
  83. c_im = zp[1];
  84. zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
  85. zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
  86. for (j = order; j >= 1; j--) {
  87. a_re = p[j][0];
  88. a_im = p[j][1];
  89. p[j][0] = a_re * zp[0] - a_im * zp[1] + p[j - 1][0];
  90. p[j][1] = a_re * zp[1] + a_im * zp[0] + p[j - 1][1];
  91. }
  92. a_re = p[0][0] * zp[0] - p[0][1] * zp[1];
  93. p[0][1] = p[0][0] * zp[1] + p[0][1] * zp[0];
  94. p[0][0] = a_re;
  95. }
  96. c->gain = p[order][0];
  97. for (i = 0; i < order; i++) {
  98. c->gain += p[i][0];
  99. c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
  100. (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
  101. }
  102. c->gain /= 1 << order;
  103. return 0;
  104. }
  105. static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
  106. enum IIRFilterMode filt_mode, int order,
  107. float cutoff_ratio, float stopband)
  108. {
  109. double cos_w0, sin_w0;
  110. double a0, x0, x1;
  111. if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
  112. filt_mode != FF_FILTER_MODE_LOWPASS) {
  113. av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
  114. "high-pass and low-pass filter modes\n");
  115. return -1;
  116. }
  117. if (order != 2) {
  118. av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
  119. return -1;
  120. }
  121. cos_w0 = cos(M_PI * cutoff_ratio);
  122. sin_w0 = sin(M_PI * cutoff_ratio);
  123. a0 = 1.0 + (sin_w0 / 2.0);
  124. if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
  125. c->gain = ((1.0 + cos_w0) / 2.0) / a0;
  126. x0 = ((1.0 + cos_w0) / 2.0) / a0;
  127. x1 = (-(1.0 + cos_w0)) / a0;
  128. } else { // FF_FILTER_MODE_LOWPASS
  129. c->gain = ((1.0 - cos_w0) / 2.0) / a0;
  130. x0 = ((1.0 - cos_w0) / 2.0) / a0;
  131. x1 = (1.0 - cos_w0) / a0;
  132. }
  133. c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
  134. c->cy[1] = (2.0 * cos_w0) / a0;
  135. // divide by gain to make the x coeffs integers.
  136. // during filtering, the delay state will include the gain multiplication
  137. c->cx[0] = lrintf(x0 / c->gain);
  138. c->cx[1] = lrintf(x1 / c->gain);
  139. return 0;
  140. }
  141. av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
  142. enum IIRFilterType filt_type,
  143. enum IIRFilterMode filt_mode,
  144. int order, float cutoff_ratio,
  145. float stopband, float ripple)
  146. {
  147. FFIIRFilterCoeffs *c;
  148. int ret = 0;
  149. if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
  150. return NULL;
  151. FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
  152. init_fail);
  153. FF_ALLOC_OR_GOTO(avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
  154. init_fail);
  155. FF_ALLOC_OR_GOTO(avc, c->cy, sizeof(c->cy[0]) * order,
  156. init_fail);
  157. c->order = order;
  158. switch (filt_type) {
  159. case FF_FILTER_TYPE_BUTTERWORTH:
  160. ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  161. stopband);
  162. break;
  163. case FF_FILTER_TYPE_BIQUAD:
  164. ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  165. stopband);
  166. break;
  167. default:
  168. av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
  169. goto init_fail;
  170. }
  171. if (!ret)
  172. return c;
  173. init_fail:
  174. ff_iir_filter_free_coeffs(c);
  175. return NULL;
  176. }
  177. av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
  178. {
  179. FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  180. return s;
  181. }
  182. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  183. #define CONV_FLT(dest, source) dest = source;
  184. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  185. in = *src0 * c->gain + \
  186. c->cy[0] * s->x[i0] + \
  187. c->cy[1] * s->x[i1] + \
  188. c->cy[2] * s->x[i2] + \
  189. c->cy[3] * s->x[i3]; \
  190. res = (s->x[i0] + in) * 1 + \
  191. (s->x[i1] + s->x[i3]) * 4 + \
  192. s->x[i2] * 6; \
  193. CONV_ ## fmt(*dst0, res) \
  194. s->x[i0] = in; \
  195. src0 += sstep; \
  196. dst0 += dstep;
  197. #define FILTER_BW_O4(type, fmt) { \
  198. int i; \
  199. const type *src0 = src; \
  200. type *dst0 = dst; \
  201. for (i = 0; i < size; i += 4) { \
  202. float in, res; \
  203. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  204. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  205. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  206. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  207. } \
  208. }
  209. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  210. int i; \
  211. const type *src0 = src; \
  212. type *dst0 = dst; \
  213. for (i = 0; i < size; i++) { \
  214. int j; \
  215. float in, res; \
  216. in = *src0 * c->gain; \
  217. for (j = 0; j < c->order; j++) \
  218. in += c->cy[j] * s->x[j]; \
  219. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  220. for (j = 1; j < c->order >> 1; j++) \
  221. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  222. for (j = 0; j < c->order - 1; j++) \
  223. s->x[j] = s->x[j + 1]; \
  224. CONV_ ## fmt(*dst0, res) \
  225. s->x[c->order - 1] = in; \
  226. src0 += sstep; \
  227. dst0 += dstep; \
  228. } \
  229. }
  230. #define FILTER_O2(type, fmt) { \
  231. int i; \
  232. const type *src0 = src; \
  233. type *dst0 = dst; \
  234. for (i = 0; i < size; i++) { \
  235. float in = *src0 * c->gain + \
  236. s->x[0] * c->cy[0] + \
  237. s->x[1] * c->cy[1]; \
  238. CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
  239. s->x[0] = s->x[1]; \
  240. s->x[1] = in; \
  241. src0 += sstep; \
  242. dst0 += dstep; \
  243. } \
  244. }
  245. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  246. struct FFIIRFilterState *s, int size,
  247. const int16_t *src, ptrdiff_t sstep,
  248. int16_t *dst, ptrdiff_t dstep)
  249. {
  250. if (c->order == 2) {
  251. FILTER_O2(int16_t, S16)
  252. } else if (c->order == 4) {
  253. FILTER_BW_O4(int16_t, S16)
  254. } else {
  255. FILTER_DIRECT_FORM_II(int16_t, S16)
  256. }
  257. }
  258. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  259. struct FFIIRFilterState *s, int size,
  260. const float *src, ptrdiff_t sstep,
  261. float *dst, ptrdiff_t dstep)
  262. {
  263. if (c->order == 2) {
  264. FILTER_O2(float, FLT)
  265. } else if (c->order == 4) {
  266. FILTER_BW_O4(float, FLT)
  267. } else {
  268. FILTER_DIRECT_FORM_II(float, FLT)
  269. }
  270. }
  271. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  272. {
  273. av_free(state);
  274. }
  275. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  276. {
  277. if (coeffs) {
  278. av_free(coeffs->cx);
  279. av_free(coeffs->cy);
  280. }
  281. av_free(coeffs);
  282. }