src/libFLAC/stream_decoder.c : Fix buffer read overflow.
[platform/upstream/flac.git] / src / libFLAC / window.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2006-2009  Josh Coalson
3  * Copyright (C) 2011-2013  Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #if HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #include <math.h>
38 #include "FLAC/assert.h"
39 #include "FLAC/format.h"
40 #include "private/window.h"
41
42 #ifndef FLAC__INTEGER_ONLY_LIBRARY
43
44 #ifndef M_PI
45 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
46 #define M_PI 3.14159265358979323846
47 #endif
48
49
50 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
51 {
52         const FLAC__int32 N = L - 1;
53         FLAC__int32 n;
54
55         if (L & 1) {
56                 for (n = 0; n <= N/2; n++)
57                         window[n] = 2.0f * n / (float)N;
58                 for (; n <= N; n++)
59                         window[n] = 2.0f - 2.0f * n / (float)N;
60         }
61         else {
62                 for (n = 0; n <= L/2-1; n++)
63                         window[n] = 2.0f * n / (float)N;
64                 for (; n <= N; n++)
65                         window[n] = 2.0f - 2.0f * (N-n) / (float)N;
66         }
67 }
68
69 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
70 {
71         const FLAC__int32 N = L - 1;
72         FLAC__int32 n;
73
74         for (n = 0; n < L; n++)
75                 window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N+0.5f) + 0.38f * cos(2.0f * M_PI * ((float)n/(float)N+0.5f)));
76 }
77
78 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
79 {
80         const FLAC__int32 N = L - 1;
81         FLAC__int32 n;
82
83         for (n = 0; n < L; n++)
84                 window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
85 }
86
87 /* 4-term -92dB side-lobe */
88 void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
89 {
90         const FLAC__int32 N = L - 1;
91         FLAC__int32 n;
92
93         for (n = 0; n <= N; n++)
94                 window[n] = (FLAC__real)(0.35875f - 0.48829f * cos(2.0f * M_PI * n / N) + 0.14128f * cos(4.0f * M_PI * n / N) - 0.01168f * cos(6.0f * M_PI * n / N));
95 }
96
97 void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
98 {
99         const FLAC__int32 N = L - 1;
100         const double N2 = (double)N / 2.;
101         FLAC__int32 n;
102
103         for (n = 0; n <= N; n++) {
104                 double k = ((double)n - N2) / N2;
105                 k = 1.0f - k * k;
106                 window[n] = (FLAC__real)(k * k);
107         }
108 }
109
110 void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
111 {
112         const FLAC__int32 N = L - 1;
113         FLAC__int32 n;
114
115         for (n = 0; n < L; n++)
116                 window[n] = (FLAC__real)(1.0f - 1.93f * cos(2.0f * M_PI * n / N) + 1.29f * cos(4.0f * M_PI * n / N) - 0.388f * cos(6.0f * M_PI * n / N) + 0.0322f * cos(8.0f * M_PI * n / N));
117 }
118
119 void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
120 {
121         const FLAC__int32 N = L - 1;
122         const double N2 = (double)N / 2.;
123         FLAC__int32 n;
124
125         for (n = 0; n <= N; n++) {
126                 const double k = ((double)n - N2) / (stddev * N2);
127                 window[n] = (FLAC__real)exp(-0.5f * k * k);
128         }
129 }
130
131 void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
132 {
133         const FLAC__int32 N = L - 1;
134         FLAC__int32 n;
135
136         for (n = 0; n < L; n++)
137                 window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
138 }
139
140 void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
141 {
142         const FLAC__int32 N = L - 1;
143         FLAC__int32 n;
144
145         for (n = 0; n < L; n++)
146                 window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
147 }
148
149 void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
150 {
151         const FLAC__int32 N = L - 1;
152         FLAC__int32 n;
153
154         for (n = 0; n < L; n++)
155                 window[n] = (FLAC__real)(0.402f - 0.498f * cos(2.0f * M_PI * n / N) + 0.098f * cos(4.0f * M_PI * n / N) - 0.001f * cos(6.0f * M_PI * n / N));
156 }
157
158 void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
159 {
160         const FLAC__int32 N = L - 1;
161         FLAC__int32 n;
162
163         for (n = 0; n < L; n++)
164                 window[n] = (FLAC__real)(0.3635819f - 0.4891775f*cos(2.0f*M_PI*n/N) + 0.1365995f*cos(4.0f*M_PI*n/N) - 0.0106411f*cos(6.0f*M_PI*n/N));
165 }
166
167 void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
168 {
169         FLAC__int32 n;
170
171         for (n = 0; n < L; n++)
172                 window[n] = 1.0f;
173 }
174
175 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
176 {
177         FLAC__int32 n;
178
179         if (L & 1) {
180                 for (n = 1; n <= L+1/2; n++)
181                         window[n-1] = 2.0f * n / ((float)L + 1.0f);
182                 for (; n <= L; n++)
183                         window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
184         }
185         else {
186                 for (n = 1; n <= L/2; n++)
187                         window[n-1] = 2.0f * n / (float)L;
188                 for (; n <= L; n++)
189                         window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L;
190         }
191 }
192
193 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
194 {
195         if (p <= 0.0)
196                 FLAC__window_rectangle(window, L);
197         else if (p >= 1.0)
198                 FLAC__window_hann(window, L);
199         else {
200                 const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
201                 FLAC__int32 n;
202                 /* start with rectangle... */
203                 FLAC__window_rectangle(window, L);
204                 /* ...replace ends with hann */
205                 if (Np > 0) {
206                         for (n = 0; n <= Np; n++) {
207                                 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
208                                 window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
209                         }
210                 }
211         }
212 }
213
214 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
215 {
216         const FLAC__int32 N = L - 1;
217         const double N2 = (double)N / 2.;
218         FLAC__int32 n;
219
220         for (n = 0; n <= N; n++) {
221                 const double k = ((double)n - N2) / N2;
222                 window[n] = (FLAC__real)(1.0f - k * k);
223         }
224 }
225
226 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */