FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
window_func.h
Go to the documentation of this file.
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 
22 #ifndef AVFILTER_WINDOW_FUNC_H
23 #define AVFILTER_WINDOW_FUNC_H
24 
25 #include <math.h>
26 #include "libavutil/avassert.h"
27 
35 
36 static inline void generate_window_func(float *lut, int N, int win_func,
37  float *overlap)
38 {
39  int n;
40 
41  switch (win_func) {
42  case WFUNC_RECT:
43  for (n = 0; n < N; n++)
44  lut[n] = 1.;
45  *overlap = 0.;
46  break;
47  case WFUNC_BARTLETT:
48  for (n = 0; n < N; n++)
49  lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
50  *overlap = 0.5;
51  break;
52  case WFUNC_HANNING:
53  for (n = 0; n < N; n++)
54  lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
55  *overlap = 0.5;
56  break;
57  case WFUNC_HAMMING:
58  for (n = 0; n < N; n++)
59  lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
60  *overlap = 0.5;
61  break;
62  case WFUNC_BLACKMAN:
63  for (n = 0; n < N; n++)
64  lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
65  *overlap = 0.661;
66  break;
67  case WFUNC_WELCH:
68  for (n = 0; n < N; n++)
69  lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
70  *overlap = 0.293;
71  break;
72  case WFUNC_FLATTOP:
73  for (n = 0; n < N; n++)
74  lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
75  1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
76  0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
77  0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
78  0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
79  *overlap = 0.841;
80  break;
81  case WFUNC_BHARRIS:
82  for (n = 0; n < N; n++)
83  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));
84  *overlap = 0.661;
85  break;
86  case WFUNC_BNUTTALL:
87  for (n = 0; n < N; n++)
88  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));
89  *overlap = 0.661;
90  break;
91  case WFUNC_BHANN:
92  for (n = 0; n < N; n++)
93  lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
94  *overlap = 0.5;
95  break;
96  case WFUNC_SINE:
97  for (n = 0; n < N; n++)
98  lut[n] = sin(M_PI*n/(N-1));
99  *overlap = 0.75;
100  break;
101  case WFUNC_NUTTALL:
102  for (n = 0; n < N; n++)
103  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));
104  *overlap = 0.663;
105  break;
106  case WFUNC_LANCZOS:
107  #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
108  for (n = 0; n < N; n++)
109  lut[n] = SINC((2.*n)/(N-1)-1);
110  *overlap = 0.75;
111  break;
112  case WFUNC_GAUSS:
113  #define SQR(x) ((x)*(x))
114  for (n = 0; n < N; n++)
115  lut[n] = exp(-0.5 * SQR((n-(N-1)/2)/(0.4*(N-1)/2.f)));
116  *overlap = 0.75;
117  break;
118  case WFUNC_TUKEY:
119  for (n = 0; n < N; n++) {
120  float M = (N-1)/2.;
121 
122  if (FFABS(n - M) >= 0.3 * M) {
123  lut[n] = 0.5 * (1 + cos((M_PI*(FFABS(n - M) - 0.3 * M))/((1 - 0.3) * M)));
124  } else {
125  lut[n] = 1;
126  }
127  }
128  *overlap = 0.33;
129  break;
130  case WFUNC_DOLPH: {
131  double b = cosh(7.6009022095419887 / (N-1)), sum, t, c, norm = 0;
132  int j;
133  for (c = 1 - 1 / (b*b), n = (N-1) / 2; n >= 0; --n) {
134  for (sum = !n, b = t = j = 1; j <= n && sum != t; b *= (n-j) * (1./j), ++j)
135  t = sum, sum += (b *= c * (N - n - j) * (1./j));
136  sum /= (N - 1 - n), sum /= (norm = norm ? norm : sum);
137  lut[n] = sum;
138  lut[N - 1 - n] = sum;
139  }
140  *overlap = 0.5;}
141  break;
142  case WFUNC_CAUCHY:
143  for (n = 0; n < N; n++) {
144  double x = 2 * ((n / (double)(N - 1)) - .5);
145 
146  if (x <= -.5 || x >= .5) {
147  lut[n] = 0;
148  } else {
149  lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
150  }
151  }
152  *overlap = 0.75;
153  break;
154  case WFUNC_PARZEN:
155  for (n = 0; n < N; n++) {
156  double x = 2 * ((n / (double)(N - 1)) - .5);
157 
158  if (x > 0.25 && x <= 0.5) {
159  lut[n] = -2 * powf(-1 + 2 * x, 3);
160  } else if (x >= -.5 && x < -.25) {
161  lut[n] = 2 * powf(1 + 2 * x, 3);
162  } else if (x >= -.25 && x < 0) {
163  lut[n] = 1 - 24 * x * x - 48 * x * x * x;
164  } else if (x >= 0 && x <= .25) {
165  lut[n] = 1 - 24 * x * x + 48 * x * x * x;
166  } else {
167  lut[n] = 0;
168  }
169  }
170  *overlap = 0.75;
171  break;
172  case WFUNC_POISSON:
173  for (n = 0; n < N; n++) {
174  double x = 2 * ((n / (double)(N - 1)) - .5);
175 
176  if (x >= 0 && x <= .5) {
177  lut[n] = exp(-6*x);
178  } else if (x < 0 && x >= -.5) {
179  lut[n] = exp(6*x);
180  } else {
181  lut[n] = 0;
182  }
183  }
184  *overlap = 0.75;
185  break;
186  case WFUNC_BOHMAN:
187  for (n = 0; n < N; n++) {
188  double x = 2 * ((n / (double)(N - 1))) - 1.;
189 
190  lut[n] = (1 - fabs(x)) * cos(M_PI*fabs(x)) + 1./M_PI*sin(M_PI*fabs(x));
191  }
192  *overlap = 0.75;
193  break;
194  default:
195  av_assert0(0);
196  }
197 }
198 
199 #endif /* AVFILTER_WINDOW_FUNC_H */
const char * b
Definition: vf_curves.c:116
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:36
#define N
Definition: af_mcompand.c:54
#define SINC(x)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define M(a, b)
Definition: vp3dsp.c:44
#define SQR(x)
#define f(width, name)
Definition: cbs_vp9.c:255
simple assert() macros that are a bit more flexible than ISO C assert().
int8_t exp
Definition: eval.c:72
#define powf(x, y)
Definition: libm.h:50
WindowFunc
#define FFMIN(a, b)
Definition: common.h:96
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int n
Definition: avisynth_c.h:684
static double c[64]
#define M_PI
Definition: mathematics.h:52