]> git.sesse.net Git - ffmpeg/blob - libavfilter/window_func.c
ops_pvq_search: remove dead macro
[ffmpeg] / libavfilter / window_func.c
1 /*
2  * Copyright (c) 2015 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <math.h>
22
23 #include "libavutil/avassert.h"
24 #include "window_func.h"
25
26 void ff_generate_window_func(float *lut, int N, int win_func, float *overlap)
27 {
28     int n;
29
30     switch (win_func) {
31     case WFUNC_RECT:
32         for (n = 0; n < N; n++)
33             lut[n] = 1.;
34         *overlap = 0.;
35         break;
36     case WFUNC_BARTLETT:
37         for (n = 0; n < N; n++)
38             lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
39         *overlap = 0.5;
40         break;
41     case WFUNC_HANNING:
42         for (n = 0; n < N; n++)
43             lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
44         *overlap = 0.5;
45         break;
46     case WFUNC_HAMMING:
47         for (n = 0; n < N; n++)
48             lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
49         *overlap = 0.5;
50         break;
51     case WFUNC_BLACKMAN:
52         for (n = 0; n < N; n++)
53             lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
54         *overlap = 0.661;
55         break;
56     case WFUNC_WELCH:
57         for (n = 0; n < N; n++)
58             lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
59         *overlap = 0.293;
60         break;
61     case WFUNC_FLATTOP:
62         for (n = 0; n < N; n++)
63             lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
64                         1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
65                         0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
66                         0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
67                         0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
68         *overlap = 0.841;
69         break;
70     case WFUNC_BHARRIS:
71         for (n = 0; n < N; n++)
72             lut[n] = 0.35875-0.48829*cos(2*M_PI*n/(N-1))+0.14128*cos(4*M_PI*n/(N-1))-0.01168*cos(6*M_PI*n/(N-1));
73         *overlap = 0.661;
74         break;
75     case WFUNC_BNUTTALL:
76         for (n = 0; n < N; n++)
77             lut[n] = 0.3635819-0.4891775*cos(2*M_PI*n/(N-1))+0.1365995*cos(4*M_PI*n/(N-1))-0.0106411*cos(6*M_PI*n/(N-1));
78         *overlap = 0.661;
79         break;
80     case WFUNC_BHANN:
81         for (n = 0; n < N; n++)
82             lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
83         *overlap = 0.5;
84         break;
85     case WFUNC_SINE:
86         for (n = 0; n < N; n++)
87             lut[n] = sin(M_PI*n/(N-1));
88         *overlap = 0.75;
89         break;
90     case WFUNC_NUTTALL:
91         for (n = 0; n < N; n++)
92             lut[n] = 0.355768-0.487396*cos(2*M_PI*n/(N-1))+0.144232*cos(4*M_PI*n/(N-1))-0.012604*cos(6*M_PI*n/(N-1));
93         *overlap = 0.663;
94         break;
95     case WFUNC_LANCZOS:
96 #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
97         for (n = 0; n < N; n++)
98             lut[n] = SINC((2.*n)/(N-1)-1);
99         *overlap = 0.75;
100         break;
101     case WFUNC_GAUSS:
102 #define SQR(x) ((x)*(x))
103         for (n = 0; n < N; n++)
104             lut[n] = exp(-0.5 * SQR((n-(N-1)/2)/(0.4*(N-1)/2.f)));
105         *overlap = 0.75;
106         break;
107     case WFUNC_TUKEY:
108         for (n = 0; n < N; n++) {
109             float M = (N-1)/2.;
110
111             if (FFABS(n - M) >= 0.3 * M) {
112                 lut[n] = 0.5 * (1 + cos((M_PI*(FFABS(n - M) - 0.3 * M))/((1 - 0.3) * M)));
113             } else {
114                 lut[n] = 1;
115             }
116         }
117         *overlap = 0.33;
118         break;
119     case WFUNC_DOLPH: {
120         double b = cosh(7.6009022095419887 / (N-1)), sum, t, c, norm = 0;
121         int j;
122         for (c = 1 - 1 / (b*b), n = (N-1) / 2; n >= 0; --n) {
123             for (sum = !n, b = t = j = 1; j <= n && sum != t; b *= (n-j) * (1./j), ++j)
124                 t = sum, sum += (b *= c * (N - n - j) * (1./j));
125             sum /= (N - 1 - n), sum /= (norm = norm ? norm : sum);
126             lut[n] = sum;
127             lut[N - 1 - n] = sum;
128         }
129         *overlap = 0.5;}
130         break;
131     case WFUNC_CAUCHY:
132         for (n = 0; n < N; n++) {
133             double x = 2 * ((n / (double)(N - 1)) - .5);
134
135             if (x <= -.5 || x >= .5) {
136                 lut[n] = 0;
137             } else {
138                 lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
139             }
140         }
141         *overlap = 0.75;
142         break;
143     case WFUNC_PARZEN:
144         for (n = 0; n < N; n++) {
145             double x = 2 * ((n / (double)(N - 1)) - .5);
146
147             if (x > 0.25 && x <= 0.5) {
148                 lut[n] = -2 * powf(-1 + 2 * x, 3);
149             } else if (x >= -.5 && x < -.25) {
150                 lut[n] = 2 * powf(1 + 2 * x, 3);
151             } else if (x >= -.25 && x < 0) {
152                 lut[n] = 1 - 24 * x * x - 48 * x * x * x;
153             } else if (x >= 0 && x <= .25) {
154                 lut[n] = 1 - 24 * x * x + 48 * x * x * x;
155             } else {
156                 lut[n] = 0;
157             }
158         }
159         *overlap = 0.75;
160         break;
161     case WFUNC_POISSON:
162         for (n = 0; n < N; n++) {
163             double x = 2 * ((n / (double)(N - 1)) - .5);
164
165             if (x >= 0 && x <= .5) {
166                 lut[n] = exp(-6*x);
167             } else if (x < 0 && x >= -.5) {
168                 lut[n] = exp(6*x);
169             } else {
170                 lut[n] = 0;
171             }
172         }
173         *overlap = 0.75;
174         break;
175     default:
176         av_assert0(0);
177     }
178 }