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.

130 lines
4.4KB

  1. /*
  2. * Lowpass 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 lowpass.c
  23. * lowpass filter implementation
  24. */
  25. #include "lowpass.h"
  26. /**********************
  27. * TODO:
  28. * support filters with order != 4
  29. * calculate coefficients for filter instead of taking approximate ones from the table
  30. *********************/
  31. /** filter order */
  32. #define LOWPASS_FILTER_ORDER 4
  33. /**
  34. * IIR filter global parameters
  35. */
  36. typedef struct FFLPFilterCoeffs{
  37. float gain;
  38. float c[LOWPASS_FILTER_ORDER];
  39. }FFLPFilterCoeffs;
  40. /**
  41. * filter data for 4th order IIR lowpass Butterworth filter
  42. */
  43. static const FFLPFilterCoeffs lp_filter_coeffs[] = {
  44. { 9.398085e-01, { -0.0176648009, 0.0000000000, -0.4860288221, 0.0000000000 } },
  45. { 6.816645e-01, { -0.4646665999, -2.2127207402, -3.9912017501, -3.2380429984 } },
  46. { 4.998150e-01, { -0.2498216698, -1.3392807613, -2.7693097862, -2.6386277439 } },
  47. { 3.103469e-01, { -0.0965076902, -0.5977763360, -1.4972580903, -1.7740085241 } },
  48. { 2.346995e-01, { -0.0557639007, -0.3623690447, -1.0304538354, -1.3066051440 } },
  49. { 1.528432e-01, { -0.0261686639, -0.1473794606, -0.6204721225, -0.6514716536 } },
  50. { 6.917529e-02, { -0.0202414073, 0.0780167640, -0.5277442247, 0.3631641670 } },
  51. { 6.178391e-02, { -0.0223681543, 0.1069446609, -0.5615167033, 0.4883976841 } },
  52. { 5.298685e-02, { -0.0261686639, 0.1473794606, -0.6204721225, 0.6514716536 } },
  53. { 2.229030e-02, { -0.0647354087, 0.4172275190, -1.1412129810, 1.4320761385 } },
  54. { 1.693903e-02, { -0.0823177861, 0.5192354923, -1.3444768251, 1.6365345642 } },
  55. { 7.374053e-03, { -0.1481421788, 0.8650973862, -1.9894244796, 2.1544844308 } },
  56. { 5.541768e-03, { -0.1742301048, 0.9921936565, -2.2090801108, 2.3024482658 } },
  57. };
  58. /** cutoff ratios for lp_filter_data[] */
  59. static const float lp_cutoff_ratios[] = {
  60. 0.5000000000, 0.4535147392, 0.4166666667, 0.3628117914,
  61. 0.3333333333, 0.2916666667, 0.2267573696, 0.2187500000,
  62. 0.2083333333, 0.1587301587, 0.1458333333, 0.1133786848,
  63. 0.1041666667,
  64. };
  65. /**
  66. * IIR filter state
  67. */
  68. typedef struct FFLPFilterState{
  69. float x[LOWPASS_FILTER_ORDER];
  70. }FFLPFilterState;
  71. const struct FFLPFilterCoeffs* ff_lowpass_filter_init_coeffs(int order, float cutoff_ratio)
  72. {
  73. int i, size;
  74. //we can create only order-4 filters with cutoff ratio <= 0.5 for now
  75. if(order != LOWPASS_FILTER_ORDER) return NULL;
  76. size = sizeof(lp_cutoff_ratios) / sizeof(lp_cutoff_ratios[0]);
  77. if(cutoff_ratio > lp_cutoff_ratios[0])
  78. return NULL;
  79. for(i = 0; i < size; i++){
  80. if(cutoff_ratio >= lp_cutoff_ratios[i])
  81. break;
  82. }
  83. if(i == size)
  84. i = size - 1;
  85. return &lp_filter_coeffs[i];
  86. }
  87. struct FFLPFilterState* ff_lowpass_filter_init_state(int order)
  88. {
  89. if(order != LOWPASS_FILTER_ORDER) return NULL;
  90. return av_mallocz(sizeof(FFLPFilterState));
  91. }
  92. #define FILTER(i0, i1, i2, i3) \
  93. in = *src * c->gain \
  94. + c->c[0]*s->x[i0] + c->c[1]*s->x[i1] \
  95. + c->c[2]*s->x[i2] + c->c[3]*s->x[i3]; \
  96. res = (s->x[i0] + in )*1 \
  97. + (s->x[i1] + s->x[i3])*4 \
  98. + s->x[i2] *6; \
  99. *dst = av_clip_int16(lrintf(res)); \
  100. s->x[i0] = in; \
  101. src += sstep; \
  102. dst += dstep; \
  103. void ff_lowpass_filter(const struct FFLPFilterCoeffs *c, struct FFLPFilterState *s, int size, int16_t *src, int sstep, int16_t *dst, int dstep)
  104. {
  105. int i;
  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. }