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.

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