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.

323 lines
12KB

  1. /*
  2. * IIR filter
  3. * Copyright (c) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (!(c = av_mallocz(sizeof(*c))) ||
  151. !(c->cx = av_malloc (sizeof(c->cx[0]) * ((order >> 1) + 1))) ||
  152. !(c->cy = av_malloc (sizeof(c->cy[0]) * order)))
  153. return NULL;
  154. c->order = order;
  155. switch (filt_type) {
  156. case FF_FILTER_TYPE_BUTTERWORTH:
  157. ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  158. stopband);
  159. break;
  160. case FF_FILTER_TYPE_BIQUAD:
  161. ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  162. stopband);
  163. break;
  164. default:
  165. av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
  166. return NULL;
  167. }
  168. if (!ret)
  169. return c;
  170. return NULL;
  171. }
  172. av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
  173. {
  174. FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  175. return s;
  176. }
  177. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  178. #define CONV_FLT(dest, source) dest = source;
  179. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  180. in = *src0 * c->gain + \
  181. c->cy[0] * s->x[i0] + \
  182. c->cy[1] * s->x[i1] + \
  183. c->cy[2] * s->x[i2] + \
  184. c->cy[3] * s->x[i3]; \
  185. res = (s->x[i0] + in) * 1 + \
  186. (s->x[i1] + s->x[i3]) * 4 + \
  187. s->x[i2] * 6; \
  188. CONV_ ## fmt(*dst0, res) \
  189. s->x[i0] = in; \
  190. src0 += sstep; \
  191. dst0 += dstep;
  192. #define FILTER_BW_O4(type, fmt) { \
  193. int i; \
  194. const type *src0 = src; \
  195. type *dst0 = dst; \
  196. for (i = 0; i < size; i += 4) { \
  197. float in, res; \
  198. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  199. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  200. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  201. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  202. } \
  203. }
  204. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  205. int i; \
  206. const type *src0 = src; \
  207. type *dst0 = dst; \
  208. for (i = 0; i < size; i++) { \
  209. int j; \
  210. float in, res; \
  211. in = *src0 * c->gain; \
  212. for (j = 0; j < c->order; j++) \
  213. in += c->cy[j] * s->x[j]; \
  214. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  215. for (j = 1; j < c->order >> 1; j++) \
  216. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  217. for (j = 0; j < c->order - 1; j++) \
  218. s->x[j] = s->x[j + 1]; \
  219. CONV_ ## fmt(*dst0, res) \
  220. s->x[c->order - 1] = in; \
  221. src0 += sstep; \
  222. dst0 += dstep; \
  223. } \
  224. }
  225. #define FILTER_O2(type, fmt) { \
  226. int i; \
  227. const type *src0 = src; \
  228. type *dst0 = dst; \
  229. for (i = 0; i < size; i++) { \
  230. float in = *src0 * c->gain + \
  231. s->x[0] * c->cy[0] + \
  232. s->x[1] * c->cy[1]; \
  233. CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
  234. s->x[0] = s->x[1]; \
  235. s->x[1] = in; \
  236. src0 += sstep; \
  237. dst0 += dstep; \
  238. } \
  239. }
  240. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  241. struct FFIIRFilterState *s, int size,
  242. const int16_t *src, ptrdiff_t sstep,
  243. int16_t *dst, ptrdiff_t dstep)
  244. {
  245. if (c->order == 2) {
  246. FILTER_O2(int16_t, S16)
  247. } else if (c->order == 4) {
  248. FILTER_BW_O4(int16_t, S16)
  249. } else {
  250. FILTER_DIRECT_FORM_II(int16_t, S16)
  251. }
  252. }
  253. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  254. struct FFIIRFilterState *s, int size,
  255. const float *src, ptrdiff_t sstep,
  256. float *dst, ptrdiff_t dstep)
  257. {
  258. if (c->order == 2) {
  259. FILTER_O2(float, FLT)
  260. } else if (c->order == 4) {
  261. FILTER_BW_O4(float, FLT)
  262. } else {
  263. FILTER_DIRECT_FORM_II(float, FLT)
  264. }
  265. }
  266. av_cold void ff_iir_filter_free_statep(struct FFIIRFilterState **state)
  267. {
  268. av_freep(state);
  269. }
  270. av_cold void ff_iir_filter_free_coeffsp(struct FFIIRFilterCoeffs **coeffsp)
  271. {
  272. struct FFIIRFilterCoeffs *coeffs = *coeffsp;
  273. if (coeffs) {
  274. av_freep(&coeffs->cx);
  275. av_freep(&coeffs->cy);
  276. }
  277. av_freep(coeffsp);
  278. }
  279. void ff_iir_filter_init(FFIIRFilterContext *f) {
  280. f->filter_flt = ff_iir_filter_flt;
  281. if (HAVE_MIPSFPU)
  282. ff_iir_filter_init_mips(f);
  283. }