FFmpeg
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 #define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func) \
37  { win_func_opt_name, "set window function", win_func_offset, AV_OPT_TYPE_INT, {.i64 = default_window_func}, 0, NB_WFUNC-1, flag, "win_func" }, \
38  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, flag, "win_func" }, \
39  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, flag, "win_func" }, \
40  { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, flag, "win_func" }, \
41  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, flag, "win_func" }, \
42  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, flag, "win_func" }, \
43  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, flag, "win_func" }, \
44  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, flag, "win_func" }, \
45  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, flag, "win_func" }, \
46  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, flag, "win_func" }, \
47  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, flag, "win_func" }, \
48  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, flag, "win_func" }, \
49  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, flag, "win_func" }, \
50  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, flag, "win_func" }, \
51  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, flag, "win_func" }, \
52  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, flag, "win_func" }, \
53  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, flag, "win_func" }, \
54  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, flag, "win_func" }, \
55  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, flag, "win_func" }, \
56  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, flag, "win_func" }, \
57  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, flag, "win_func" }, \
58  { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, flag, "win_func" }
59 
60 static inline void generate_window_func(float *lut, int N, int win_func,
61  float *overlap)
62 {
63  int n;
64 
65  switch (win_func) {
66  case WFUNC_RECT:
67  for (n = 0; n < N; n++)
68  lut[n] = 1.;
69  *overlap = 0.;
70  break;
71  case WFUNC_BARTLETT:
72  for (n = 0; n < N; n++)
73  lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
74  *overlap = 0.5;
75  break;
76  case WFUNC_HANNING:
77  for (n = 0; n < N; n++)
78  lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
79  *overlap = 0.5;
80  break;
81  case WFUNC_HAMMING:
82  for (n = 0; n < N; n++)
83  lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
84  *overlap = 0.5;
85  break;
86  case WFUNC_BLACKMAN:
87  for (n = 0; n < N; n++)
88  lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
89  *overlap = 0.661;
90  break;
91  case WFUNC_WELCH:
92  for (n = 0; n < N; n++)
93  lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
94  *overlap = 0.293;
95  break;
96  case WFUNC_FLATTOP:
97  for (n = 0; n < N; n++)
98  lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
99  1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
100  0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
101  0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
102  0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
103  *overlap = 0.841;
104  break;
105  case WFUNC_BHARRIS:
106  for (n = 0; n < N; n++)
107  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));
108  *overlap = 0.661;
109  break;
110  case WFUNC_BNUTTALL:
111  for (n = 0; n < N; n++)
112  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));
113  *overlap = 0.661;
114  break;
115  case WFUNC_BHANN:
116  for (n = 0; n < N; n++)
117  lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
118  *overlap = 0.5;
119  break;
120  case WFUNC_SINE:
121  for (n = 0; n < N; n++)
122  lut[n] = sin(M_PI*n/(N-1));
123  *overlap = 0.75;
124  break;
125  case WFUNC_NUTTALL:
126  for (n = 0; n < N; n++)
127  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));
128  *overlap = 0.663;
129  break;
130  case WFUNC_LANCZOS:
131  #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
132  for (n = 0; n < N; n++)
133  lut[n] = SINC((2.*n)/(N-1)-1);
134  *overlap = 0.75;
135  break;
136  case WFUNC_GAUSS:
137  #define SQR(x) ((x)*(x))
138  for (n = 0; n < N; n++)
139  lut[n] = exp(-0.5 * SQR((n-(N-1)/2)/(0.4*(N-1)/2.f)));
140  *overlap = 0.75;
141  break;
142  case WFUNC_TUKEY:
143  for (n = 0; n < N; n++) {
144  float M = (N-1)/2.;
145 
146  if (FFABS(n - M) >= 0.3 * M) {
147  lut[n] = 0.5 * (1 + cos((M_PI*(FFABS(n - M) - 0.3 * M))/((1 - 0.3) * M)));
148  } else {
149  lut[n] = 1;
150  }
151  }
152  *overlap = 0.33;
153  break;
154  case WFUNC_DOLPH: {
155  double b = cosh(7.6009022095419887 / (N-1)), sum, t, c, norm = 0;
156  int j;
157  for (c = 1 - 1 / (b*b), n = (N-1) / 2; n >= 0; --n) {
158  for (sum = !n, b = t = j = 1; j <= n && sum != t; b *= (n-j) * (1./j), ++j)
159  t = sum, sum += (b *= c * (N - n - j) * (1./j));
160  sum /= (N - 1 - n), norm = norm ? norm : sum, sum /= norm;
161  lut[n] = sum;
162  lut[N - 1 - n] = sum;
163  }
164  *overlap = 0.5;}
165  break;
166  case WFUNC_CAUCHY:
167  for (n = 0; n < N; n++) {
168  double x = 2 * ((n / (double)(N - 1)) - .5);
169 
170  if (x <= -.5 || x >= .5) {
171  lut[n] = 0;
172  } else {
173  lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
174  }
175  }
176  *overlap = 0.75;
177  break;
178  case WFUNC_PARZEN:
179  for (n = 0; n < N; n++) {
180  double x = 2 * ((n / (double)(N - 1)) - .5);
181 
182  if (x > 0.25 && x <= 0.5) {
183  lut[n] = -2 * powf(-1 + 2 * x, 3);
184  } else if (x >= -.5 && x < -.25) {
185  lut[n] = 2 * powf(1 + 2 * x, 3);
186  } else if (x >= -.25 && x < 0) {
187  lut[n] = 1 - 24 * x * x - 48 * x * x * x;
188  } else if (x >= 0 && x <= .25) {
189  lut[n] = 1 - 24 * x * x + 48 * x * x * x;
190  } else {
191  lut[n] = 0;
192  }
193  }
194  *overlap = 0.75;
195  break;
196  case WFUNC_POISSON:
197  for (n = 0; n < N; n++) {
198  double x = 2 * ((n / (double)(N - 1)) - .5);
199 
200  if (x >= 0 && x <= .5) {
201  lut[n] = exp(-6*x);
202  } else if (x < 0 && x >= -.5) {
203  lut[n] = exp(6*x);
204  } else {
205  lut[n] = 0;
206  }
207  }
208  *overlap = 0.75;
209  break;
210  case WFUNC_BOHMAN:
211  for (n = 0; n < N; n++) {
212  double x = 2 * ((n / (double)(N - 1))) - 1.;
213 
214  lut[n] = (1 - fabs(x)) * cos(M_PI*fabs(x)) + 1./M_PI*sin(M_PI*fabs(x));
215  }
216  *overlap = 0.75;
217  break;
218  default:
219  av_assert0(0);
220  }
221 }
222 
223 #endif /* AVFILTER_WINDOW_FUNC_H */
M
#define M(a, b)
Definition: vp3dsp.c:48
b
#define b
Definition: input.c:40
WFUNC_BNUTTALL
@ WFUNC_BNUTTALL
Definition: window_func.h:30
WFUNC_FLATTOP
@ WFUNC_FLATTOP
Definition: window_func.h:29
WFUNC_HAMMING
@ WFUNC_HAMMING
Definition: window_func.h:28
WFUNC_PARZEN
@ WFUNC_PARZEN
Definition: window_func.h:32
WindowFunc
WindowFunc
Definition: af_firequalizer.c:33
WFUNC_TUKEY
@ WFUNC_TUKEY
Definition: window_func.h:31
WFUNC_BHANN
@ WFUNC_BHANN
Definition: window_func.h:31
avassert.h
WFUNC_DOLPH
@ WFUNC_DOLPH
Definition: window_func.h:32
WFUNC_BHARRIS
@ WFUNC_BHARRIS
Definition: window_func.h:30
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
WFUNC_LANCZOS
@ WFUNC_LANCZOS
Definition: window_func.h:31
WFUNC_RECT
@ WFUNC_RECT
Definition: window_func.h:28
f
#define f(width, name)
Definition: cbs_vp9.c:255
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
NB_WFUNC
@ NB_WFUNC
Definition: window_func.h:34
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
generate_window_func
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:60
WFUNC_HANNING
@ WFUNC_HANNING
Definition: window_func.h:28
WFUNC_BARTLETT
@ WFUNC_BARTLETT
Definition: window_func.h:29
WFUNC_BOHMAN
@ WFUNC_BOHMAN
Definition: window_func.h:33
exp
int8_t exp
Definition: eval.c:72
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
powf
#define powf(x, y)
Definition: libm.h:50
N
#define N
Definition: af_mcompand.c:53
M_PI
#define M_PI
Definition: mathematics.h:52
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
WFUNC_SINE
@ WFUNC_SINE
Definition: window_func.h:30
WFUNC_CAUCHY
@ WFUNC_CAUCHY
Definition: window_func.h:32
WFUNC_GAUSS
@ WFUNC_GAUSS
Definition: window_func.h:31
WFUNC_NUTTALL
@ WFUNC_NUTTALL
Definition: window_func.h:30
WFUNC_POISSON
@ WFUNC_POISSON
Definition: window_func.h:32
SQR
#define SQR(x)
SINC
#define SINC(x)
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: window_func.h:28
WFUNC_WELCH
@ WFUNC_WELCH
Definition: window_func.h:29