/src/faac/libfaad/imdct.c
Line | Count | Source |
1 | | /* |
2 | | * FAAD - Freeware Advanced Audio Decoder |
3 | | * Copyright (C) 2026 Nils Schimmelmann |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | */ |
15 | | |
16 | | #include "faad_internal.h" |
17 | | #include "fft.h" |
18 | | |
19 | | #ifndef M_PI |
20 | | #define M_PI 3.14159265358979323846 |
21 | | #endif |
22 | | |
23 | | static float kbd_window_2048[1024]; |
24 | | static float sine_window_2048[1024]; |
25 | | static float kbd_window_256[128]; |
26 | | static float sine_window_256[128]; |
27 | | |
28 | | #ifndef FAAD_DISABLE_DAB |
29 | | static float kbd_window_1920[960]; |
30 | | static float sine_window_1920[960]; |
31 | | static float kbd_window_240[120]; |
32 | | static float sine_window_240[120]; |
33 | | #endif |
34 | | |
35 | | /* DCT-IV twiddles exp(-j*pi*(n + 1/8)/M) */ |
36 | | static float dct4_cos_1024[512]; |
37 | | static float dct4_sin_1024[512]; |
38 | | static float dct4_cos_128[64]; |
39 | | static float dct4_sin_128[64]; |
40 | | |
41 | | #ifndef FAAD_DISABLE_DAB |
42 | | static float dct4_cos_960[480]; |
43 | | static float dct4_sin_960[480]; |
44 | | static float dct4_cos_120[60]; |
45 | | static float dct4_sin_120[60]; |
46 | | #endif |
47 | | |
48 | | |
49 | | static double bessel_i0(double x) |
50 | 4.46k | { |
51 | 4.46k | double sum = 1.0, term = 1.0, q = x * x / 4.0; |
52 | 108k | for (int k = 1; k < 60; k++) { |
53 | 108k | term *= q / ((double)k * k); |
54 | 108k | sum += term; |
55 | 108k | if (term < sum * 1e-17) break; |
56 | 108k | } |
57 | 4.46k | return sum; |
58 | 4.46k | } |
59 | | |
60 | | /* Kaiser-Bessel-derived window, first half (ISO/IEC 14496-3 §4.6.11.3.2): |
61 | | * the cumulative Kaiser kernel, normalized, under a square root. */ |
62 | | static void kbd_window(float *w, int n, double alpha) |
63 | 4 | { |
64 | 4 | double sum = 0.0, run = 0.0; |
65 | 2.24k | for (int i = 0; i <= n; i++) { |
66 | 2.23k | double v = (2.0 * i / n) - 1.0; |
67 | 2.23k | sum += bessel_i0(M_PI * alpha * sqrt(1.0 - v * v)); |
68 | 2.23k | } |
69 | 2.23k | for (int i = 0; i < n; i++) { |
70 | 2.23k | double v = (2.0 * i / n) - 1.0; |
71 | 2.23k | run += bessel_i0(M_PI * alpha * sqrt(1.0 - v * v)); |
72 | 2.23k | w[i] = (float)sqrt(run / sum); |
73 | 2.23k | } |
74 | 4 | } |
75 | | |
76 | | void init_windows(void) |
77 | 1 | { |
78 | 1 | fft_init(); |
79 | | |
80 | 1.02k | for (int i = 0; i < 1024; i++) { |
81 | 1.02k | sine_window_2048[i] = sinf((float)M_PI * (i + 0.5f) / 2048.0f); |
82 | 1.02k | } |
83 | 129 | for (int i = 0; i < 128; i++) { |
84 | 128 | sine_window_256[i] = sinf((float)M_PI * (i + 0.5f) / 256.0f); |
85 | 128 | } |
86 | 1 | #ifndef FAAD_DISABLE_DAB |
87 | 961 | for (int i = 0; i < 960; i++) { |
88 | 960 | sine_window_1920[i] = sinf((float)M_PI * (i + 0.5f) / 1920.0f); |
89 | 960 | } |
90 | 121 | for (int i = 0; i < 120; i++) { |
91 | 120 | sine_window_240[i] = sinf((float)M_PI * (i + 0.5f) / 240.0f); |
92 | 120 | } |
93 | 1 | #endif |
94 | | |
95 | 1 | kbd_window(kbd_window_2048, 1024, 4.0); |
96 | 1 | kbd_window(kbd_window_256, 128, 6.0); |
97 | 1 | #ifndef FAAD_DISABLE_DAB |
98 | 1 | kbd_window(kbd_window_1920, 960, 4.0); |
99 | 1 | kbd_window(kbd_window_240, 120, 6.0); |
100 | 1 | #endif |
101 | | |
102 | 513 | for (int k = 0; k < 512; k++) { |
103 | 512 | double ang = -M_PI * (k + 0.125) / 1024.0; |
104 | 512 | dct4_cos_1024[k] = (float)cos(ang); |
105 | 512 | dct4_sin_1024[k] = (float)sin(ang); |
106 | 512 | } |
107 | 65 | for (int k = 0; k < 64; k++) { |
108 | 64 | double ang = -M_PI * (k + 0.125) / 128.0; |
109 | 64 | dct4_cos_128[k] = (float)cos(ang); |
110 | 64 | dct4_sin_128[k] = (float)sin(ang); |
111 | 64 | } |
112 | 1 | #ifndef FAAD_DISABLE_DAB |
113 | 481 | for (int k = 0; k < 480; k++) { |
114 | 480 | double ang = -M_PI * (k + 0.125) / 960.0; |
115 | 480 | dct4_cos_960[k] = (float)cos(ang); |
116 | 480 | dct4_sin_960[k] = (float)sin(ang); |
117 | 480 | } |
118 | 61 | for (int k = 0; k < 60; k++) { |
119 | 60 | double ang = -M_PI * (k + 0.125) / 120.0; |
120 | 60 | dct4_cos_120[k] = (float)cos(ang); |
121 | 60 | dct4_sin_120[k] = (float)sin(ang); |
122 | 60 | } |
123 | 1 | #endif |
124 | 1 | } |
125 | | |
126 | | /* DCT-IV of length M through an M/2-point complex FFT: pack even-index |
127 | | * inputs against reversed odd-index inputs, rotate, transform, rotate again |
128 | | * and unzip. buf holds 2*M floats: z, which the result u then overwrites |
129 | | * (the FFT is done with it by then), and w. The result is buf[0..M). */ |
130 | | static void dct4(const float * restrict in, float * restrict buf, int M) |
131 | 239k | { |
132 | 239k | int K = M / 2; |
133 | 239k | #ifndef FAAD_DISABLE_DAB |
134 | 239k | const float *cs = (M == 1024) ? dct4_cos_1024 : ((M == 960) ? dct4_cos_960 : ((M == 128) ? dct4_cos_128 : dct4_cos_120)); |
135 | 239k | const float *sn = (M == 1024) ? dct4_sin_1024 : ((M == 960) ? dct4_sin_960 : ((M == 128) ? dct4_sin_128 : dct4_sin_120)); |
136 | | #else |
137 | | const float *cs = (M == 1024) ? dct4_cos_1024 : dct4_cos_128; |
138 | | const float *sn = (M == 1024) ? dct4_sin_1024 : dct4_sin_128; |
139 | | #endif |
140 | 239k | float *z = buf, *w = buf + M, *u = buf; |
141 | 239k | float *zr = z, *zi = z + K; |
142 | | |
143 | 72.4M | for (int n = 0; n < K; n++) { |
144 | 72.2M | float a = in[2 * n], b = in[M - 1 - 2 * n]; |
145 | 72.2M | zr[n] = a * cs[n] - b * sn[n]; |
146 | 72.2M | zi[n] = a * sn[n] + b * cs[n]; |
147 | 72.2M | } |
148 | 239k | #ifndef FAAD_DISABLE_DAB |
149 | 239k | if (M == 1024) fft(z, w, 9); |
150 | 112k | else if (M == 960) fft480(z, w); |
151 | 112k | else if (M == 128) fft(z, w, 6); |
152 | 0 | else fft60(z, w); |
153 | | #else |
154 | | fft(z, w, (M == 1024) ? 9 : 6); |
155 | | #endif |
156 | | |
157 | 239k | const float *wr = w, *wi = w + K; |
158 | 72.4M | for (int k = 0; k < K; k++) { |
159 | 72.2M | u[2 * k] = wr[k] * cs[k] - wi[k] * sn[k]; |
160 | 72.2M | u[M - 1 - 2 * k] = -(wr[k] * sn[k] + wi[k] * cs[k]); |
161 | 72.2M | } |
162 | 239k | } |
163 | | |
164 | | /* IMDCT (ISO/IEC 14496-3 §4.6.11.3.1): n0 = N/4 + 1/2 makes the transform a |
165 | | * DCT-IV of the coefficients, folded out with its odd/even symmetries. */ |
166 | | static void fast_imdct(const float * restrict in, float * restrict out, int n, float * restrict tmp) |
167 | 112k | { |
168 | 112k | int M = n / 2, H = M / 2; |
169 | 112k | float *u = tmp; /* 2*M floats of work */ |
170 | 112k | float scale = 2.0f / (float)n; |
171 | | |
172 | 112k | dct4(in, tmp, M); |
173 | 7.33M | for (int i = 0; i < H; i++) { |
174 | 7.22M | out[i] = u[H + i] * scale; |
175 | 7.22M | out[M + H + i] = -u[i] * scale; |
176 | 7.22M | } |
177 | 14.5M | for (int i = H; i < M + H; i++) |
178 | 14.4M | out[i] = -u[M + H - 1 - i] * scale; |
179 | 112k | } |
180 | | |
181 | | /* The 2048-sample IMDCT output, folded out of the 1024-point DCT-IV u (see |
182 | | * fast_imdct), sample i, scaled. */ |
183 | | static inline float imdct_sample(const float *u, int i, float scale, int frame_len_long) |
184 | 12.3M | { |
185 | 12.3M | if (frame_len_long == 960) { |
186 | 0 | if (i < 480) return u[480 + i] * scale; |
187 | 0 | if (i < 1440) return -u[1439 - i] * scale; |
188 | 0 | return -u[i - 1440] * scale; |
189 | 0 | } |
190 | 12.3M | if (i < 512) return u[512 + i] * scale; |
191 | 10.3M | if (i < 1536) return -u[1535 - i] * scale; |
192 | 2.43M | return -u[i - 1536] * scale; |
193 | 10.3M | } |
194 | | |
195 | | /* Left half: window, add the previous frame's overlap, emit. Right half: |
196 | | * window into the overlap for the next frame. */ |
197 | | static inline void imdct_emit(float * restrict out_pcm, float * restrict overlap, const float *u, float scale, |
198 | | int i0, int i1, const float * restrict wl, int wl_dir, float wflat, int frame_len_long) |
199 | 23.2k | { |
200 | | /* window sample i is wl[i - i0] (wl_dir > 0), wl[i1 - 1 - i] (< 0) or wflat (wl == NULL) */ |
201 | 12.4M | for (int i = i0; i < i1; i++) { |
202 | 12.3M | float w = wl ? (wl_dir > 0 ? wl[i - i0] : wl[i1 - 1 - i]) : wflat; |
203 | 12.3M | float x = imdct_sample(u, i, scale, frame_len_long) * w; |
204 | 12.3M | if (i < frame_len_long) out_pcm[i] = x + overlap[i]; |
205 | 6.39M | else overlap[i - frame_len_long] = x; |
206 | 12.3M | } |
207 | 23.2k | } |
208 | | |
209 | | /* imdct_emit() for the common long-long block, cut where the fold and the |
210 | | * emit/overlap split change so each piece is a straight, vectorisable loop. */ |
211 | | static void imdct_emit_long(float * restrict out_pcm, float * restrict overlap, const float * restrict u, float scale, |
212 | | const float * restrict wl, const float * restrict wr) |
213 | 119k | { |
214 | 61.1M | for (int i = 0; i < 512; i++) out_pcm[i] = u[512 + i] * scale * wl[i] + overlap[i]; |
215 | 61.1M | for (int i = 512; i < 1024; i++) out_pcm[i] = -u[1535 - i] * scale * wl[i] + overlap[i]; |
216 | 61.1M | for (int i = 1024; i < 1536; i++) overlap[i - 1024] = -u[1535 - i] * scale * wr[2047 - i]; |
217 | 61.1M | for (int i = 1536; i < 2048; i++) overlap[i - 1024] = -u[i - 1536] * scale * wr[2047 - i]; |
218 | 119k | } |
219 | | |
220 | | void imdct_and_window(struct faad_decoder *dec, uint32_t ch, uint8_t sequence, uint8_t shape, |
221 | | float * restrict spec, float * restrict pcm) |
222 | 140k | { |
223 | 140k | imdct_window(dec, sequence, shape, dec->prev_window_shape[ch], dec->overlap[ch], spec, pcm); |
224 | 140k | dec->prev_window_shape[ch] = shape; |
225 | 140k | dec->prev_window_seq[ch] = sequence; |
226 | 140k | } |
227 | | |
228 | 141k | #define IMDCT_FRAME_LEN 1024 |
229 | | #define IMDCT_WINDOW_FN imdct_window_1024 |
230 | | #include "imdct_window.inc" |
231 | | #undef IMDCT_FRAME_LEN |
232 | | #undef IMDCT_WINDOW_FN |
233 | | |
234 | | #ifndef FAAD_DISABLE_DAB |
235 | 0 | #define IMDCT_FRAME_LEN 960 |
236 | | #define IMDCT_WINDOW_FN imdct_window_960 |
237 | | #include "imdct_window.inc" |
238 | | #undef IMDCT_FRAME_LEN |
239 | | #undef IMDCT_WINDOW_FN |
240 | | #endif |
241 | | |
242 | | void imdct_window(struct faad_decoder *dec, uint8_t window_sequence, uint8_t window_shape, |
243 | | uint8_t prev_shape, float * restrict overlap, |
244 | | float * restrict spec, float * restrict out_pcm) |
245 | 141k | { |
246 | 141k | #ifndef FAAD_DISABLE_DAB |
247 | 141k | if (FRAME_IS_960(dec)) { |
248 | 0 | imdct_window_960(dec, window_sequence, window_shape, prev_shape, overlap, spec, out_pcm); |
249 | 0 | return; |
250 | 0 | } |
251 | 141k | #endif |
252 | 141k | imdct_window_1024(dec, window_sequence, window_shape, prev_shape, overlap, spec, out_pcm); |
253 | 141k | } |