iirfilter.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 libavcodec/iirfilter.c
  23. * different IIR filters implementation
  24. */
  25. #include "iirfilter.h"
  26. #include <complex.h>
  27. #include <math.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. struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
  46. enum IIRFilterMode filt_mode,
  47. int order, float cutoff_ratio,
  48. float stopband, float ripple)
  49. {
  50. int i, j, size;
  51. FFIIRFilterCoeffs *c;
  52. double wa;
  53. complex p[MAXORDER + 1];
  54. if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
  55. return NULL;
  56. if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
  57. return NULL;
  58. c = av_malloc(sizeof(FFIIRFilterCoeffs));
  59. c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
  60. c->cy = av_malloc(sizeof(c->cy[0]) * order);
  61. c->order = order;
  62. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  63. c->cx[0] = 1;
  64. for(i = 1; i < (order >> 1) + 1; i++)
  65. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  66. p[0] = 1.0;
  67. for(i = 1; i <= order; i++)
  68. p[i] = 0.0;
  69. for(i = 0; i < order; i++){
  70. complex zp;
  71. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  72. zp = cexp(I*th) * wa;
  73. zp = (zp + 2.0) / (zp - 2.0);
  74. for(j = order; j >= 1; j--)
  75. p[j] = zp*p[j] + p[j - 1];
  76. p[0] *= zp;
  77. }
  78. c->gain = creal(p[order]);
  79. for(i = 0; i < order; i++){
  80. c->gain += creal(p[i]);
  81. c->cy[i] = creal(-p[i] / p[order]);
  82. }
  83. c->gain /= 1 << order;
  84. return c;
  85. }
  86. struct FFIIRFilterState* ff_iir_filter_init_state(int order)
  87. {
  88. FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  89. return s;
  90. }
  91. #define FILTER(i0, i1, i2, i3) \
  92. in = *src * c->gain \
  93. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  94. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  95. res = (s->x[i0] + in )*1 \
  96. + (s->x[i1] + s->x[i3])*4 \
  97. + s->x[i2] *6; \
  98. *dst = av_clip_int16(lrintf(res)); \
  99. s->x[i0] = in; \
  100. src += sstep; \
  101. dst += dstep; \
  102. void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
  103. {
  104. int i;
  105. if(c->order == 4){
  106. for(i = 0; i < size; i += 4){
  107. float in, res;
  108. FILTER(0, 1, 2, 3);
  109. FILTER(1, 2, 3, 0);
  110. FILTER(2, 3, 0, 1);
  111. FILTER(3, 0, 1, 2);
  112. }
  113. }else{
  114. for(i = 0; i < size; i++){
  115. int j;
  116. float in, res;
  117. in = *src * c->gain;
  118. for(j = 0; j < c->order; j++)
  119. in += c->cy[j] * s->x[j];
  120. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
  121. for(j = 1; j < c->order >> 1; j++)
  122. res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
  123. for(j = 0; j < c->order - 1; j++)
  124. s->x[j] = s->x[j + 1];
  125. *dst = av_clip_int16(lrintf(res));
  126. s->x[c->order - 1] = in;
  127. src += sstep;
  128. dst += sstep;
  129. }
  130. }
  131. }
  132. void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  133. {
  134. av_free(state);
  135. }
  136. void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  137. {
  138. if(coeffs){
  139. av_free(coeffs->cx);
  140. av_free(coeffs->cy);
  141. }
  142. av_free(coeffs);
  143. }