* builds a polyphase filterbank. * @param factor resampling factor * @param scale wanted sum of coefficients for each filter * @param filter_type filter type * @param kaiser_beta kaiser window beta * @return 0 on success, negative on error */
| 39 | * @return 0 on success, negative on error |
| 40 | */ |
| 41 | static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale, |
| 42 | int filter_type, double kaiser_beta){ |
| 43 | int ph, i; |
| 44 | int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1; |
| 45 | double x, y, w, t, s; |
| 46 | double *tab = av_malloc_array(tap_count+1, sizeof(*tab)); |
| 47 | double *sin_lut = av_malloc_array(ph_nb, sizeof(*sin_lut)); |
| 48 | const int center= (tap_count-1)/2; |
| 49 | double norm = 0; |
| 50 | int ret = AVERROR(ENOMEM); |
| 51 | |
| 52 | if (!tab || !sin_lut) |
| 53 | goto fail; |
| 54 | |
| 55 | av_assert0(tap_count == 1 || tap_count % 2 == 0); |
| 56 | |
| 57 | /* if upsampling, only need to interpolate, no filter */ |
| 58 | if (factor > 1.0) |
| 59 | factor = 1.0; |
| 60 | |
| 61 | if (factor == 1.0) { |
| 62 | for (ph = 0; ph < ph_nb; ph++) |
| 63 | sin_lut[ph] = sin(M_PI * ph / phase_count) * (center & 1 ? 1 : -1); |
| 64 | } |
| 65 | for(ph = 0; ph < ph_nb; ph++) { |
| 66 | s = sin_lut[ph]; |
| 67 | for(i=0;i<tap_count;i++) { |
| 68 | x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor; |
| 69 | if (x == 0) y = 1.0; |
| 70 | else if (factor == 1.0) |
| 71 | y = s / x; |
| 72 | else |
| 73 | y = sin(x) / x; |
| 74 | switch(filter_type){ |
| 75 | case SWR_FILTER_TYPE_CUBIC:{ |
| 76 | const float d= -0.5; //first order derivative = -0.5 |
| 77 | x = fabs(((double)(i - center) - (double)ph / phase_count) * factor); |
| 78 | if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x); |
| 79 | else y= d*(-4 + 8*x - 5*x*x + x*x*x); |
| 80 | break;} |
| 81 | case SWR_FILTER_TYPE_BLACKMAN_NUTTALL: |
| 82 | w = 2.0*x / (factor*tap_count); |
| 83 | t = -cos(w); |
| 84 | y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t); |
| 85 | break; |
| 86 | case SWR_FILTER_TYPE_KAISER: |
| 87 | w = 2.0*x / (factor*tap_count*M_PI); |
| 88 | y *= av_bessel_i0(kaiser_beta*sqrt(FFMAX(1-w*w, 0))); |
| 89 | break; |
| 90 | default: |
| 91 | av_assert0(0); |
| 92 | } |
| 93 | |
| 94 | tab[i] = y; |
| 95 | s = -s; |
| 96 | if (!ph) |
| 97 | norm += y; |
| 98 | } |
no test coverage detected