/proc/self/cwd/external/faac/libfaar/dft_fir.c
Line | Count | Source |
1 | | /* |
2 | | * FAAR - Freeware Advanced Audio Resampler |
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 | | * Overlap-save FIR stage used by FAAR. The block geometry follows standard |
17 | | * linear convolution. |
18 | | */ |
19 | | |
20 | | #include "dft_fir.h" |
21 | | |
22 | | #include <limits.h> |
23 | | #include <math.h> |
24 | | #include <stdint.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | static int init_stage(faar_dft_fir *stage, const double *taps, |
29 | | size_t tap_count, size_t fft_size, size_t expansion, |
30 | | size_t decimation, size_t lanes, int stacked, int compensated); |
31 | | |
32 | | |
33 | | int faar_dft_fir_init_paired(faar_dft_fir *stage, const double *taps, |
34 | | size_t tap_count, size_t fft_size, size_t expansion, |
35 | 0 | size_t decimation, size_t lanes, int stacked) { |
36 | 0 | return init_stage(stage, taps, tap_count, fft_size, expansion, |
37 | 0 | decimation, lanes, stacked, 0); |
38 | 0 | } |
39 | | |
40 | 0 | static size_t overlap_stride(const faar_dft_fir *stage) { |
41 | 0 | size_t stored = (stage->tap_count - 1) / stage->expansion; |
42 | 0 | return stored ? stored : 1; |
43 | 0 | } |
44 | | |
45 | 0 | #define PAIR_REAL faar_sample_t |
46 | | #define PAIR_PLAN faar_fft_plan |
47 | | #define PAIR_TAG |
48 | | #define PAIR_FFT faar_fft_ |
49 | | #include "dft_fir_pair.inc" |
50 | | #undef PAIR_REAL |
51 | | #undef PAIR_PLAN |
52 | | #undef PAIR_TAG |
53 | | #undef PAIR_FFT |
54 | | #if !FAAR_RESAMPLER_FLOAT |
55 | 0 | #define PAIR_REAL double |
56 | | #define PAIR_PLAN faar_fftd_plan |
57 | | #define PAIR_TAG _wide |
58 | | #define PAIR_FFT faar_fftd_ |
59 | | #include "dft_fir_pair.inc" |
60 | | #undef PAIR_REAL |
61 | | #undef PAIR_PLAN |
62 | | #undef PAIR_TAG |
63 | | #undef PAIR_FFT |
64 | | |
65 | | /* Filter spectrum in the e^{-j} convention, pre-scaled by the inverse FFT |
66 | | * and decimation averaging. Both scales are powers of two, so exact. */ |
67 | 0 | static int init_compensated_spectrum(faar_dft_fir *stage, const double *taps) { |
68 | 0 | const size_t n = stage->fft_size, nout = n / stage->decimation; |
69 | 0 | const double scale = 1.0 / ((double)nout * (double)stage->decimation); |
70 | 0 | faar_fftd_plan plan; |
71 | 0 | double *h, *spectrum; |
72 | 0 | if (n > (size_t)INT_MAX || faar_fftd_cinit(&plan, (int)n)) return -1; |
73 | 0 | h = faar_fftd_cbuffer(&plan, 0); |
74 | 0 | memset(h, 0, 2 * n * sizeof(double)); |
75 | 0 | for (size_t i = 0; i < stage->tap_count; ++i) h[i] = taps[i]; |
76 | 0 | spectrum = (double *)AllocMemory(2 * n * sizeof(double)); |
77 | 0 | if (!spectrum) { faar_fftd_clear(&plan); return -1; } |
78 | 0 | h = faar_fftd_cforward(&plan, h); |
79 | 0 | for (size_t i = 0; i < 2 * n; ++i) spectrum[i] = h[i] * scale; |
80 | 0 | faar_fftd_clear(&plan); |
81 | 0 | stage->spec_wide = spectrum; |
82 | 0 | return init_fft_plans_wide(stage); |
83 | 0 | } |
84 | | #endif |
85 | | #include "dft_fir_float2.inc" |
86 | | #include "dft_fir_wide.inc" |
87 | | |
88 | 0 | static int init_paired_buffers(faar_dft_fir *stage) { |
89 | 0 | stage->overlap = (faar_sample_t *)faar_alloc_zeroed(stage->lanes * overlap_stride(stage), |
90 | 0 | sizeof(faar_sample_t)); |
91 | 0 | stage->input_block = (faar_sample_t *)faar_alloc_zeroed(stage->lanes |
92 | 0 | * stage->stream_in, sizeof(faar_sample_t)); |
93 | 0 | stage->output_block = (faar_sample_t *)faar_alloc_zeroed(stage->lanes |
94 | 0 | * stage->stream_out, sizeof(faar_sample_t)); |
95 | 0 | return stage->overlap && stage->input_block && stage->output_block |
96 | 0 | ? 0 : -1; |
97 | 0 | } |
98 | | |
99 | | /* High's wide stages keep their spectrum only in double. */ |
100 | 0 | static const void *paired_spectrum(const faar_dft_fir *stage) { |
101 | 0 | #if !FAAR_RESAMPLER_FLOAT |
102 | 0 | if (stage->compensated) return stage->spec_wide; |
103 | 0 | #endif |
104 | 0 | return stage->spec; |
105 | 0 | } |
106 | | |
107 | | /* Spectrum of the taps in the e^{-j} convention, split into real then |
108 | | * imaginary parts. A float transform leaves about 2^-24 of absolute error |
109 | | * in every stopband bin, which caps image rejection near 145 dB, so this |
110 | | * setup-only radix-2 transform runs in double and rounds once. */ |
111 | | static int double_spectrum(faar_sample_t *out, const double *taps, |
112 | 0 | size_t tap_count, size_t n, double scale) { |
113 | 0 | double *re = (double *)faar_alloc_zeroed(3 * n, sizeof(*re)); |
114 | 0 | if (!re) return -1; |
115 | 0 | double *im = re + n, *tw = im + n; |
116 | 0 | memcpy(re, taps, tap_count * sizeof(*re)); |
117 | 0 | for (size_t k = 0; k < n / 2; ++k) { |
118 | 0 | const double angle = -2.0 * 3.14159265358979323846 * (double)k / (double)n; |
119 | 0 | tw[2 * k] = cos(angle); |
120 | 0 | tw[2 * k + 1] = sin(angle); |
121 | 0 | } |
122 | 0 | for (size_t i = 1, j = 0; i < n; ++i) { |
123 | 0 | size_t bit = n >> 1; |
124 | 0 | for (; j & bit; bit >>= 1) j ^= bit; |
125 | 0 | j ^= bit; |
126 | 0 | if (i < j) { |
127 | 0 | double t = re[i]; re[i] = re[j]; re[j] = t; |
128 | 0 | } |
129 | 0 | } |
130 | 0 | for (size_t len = 2; len <= n; len <<= 1) { |
131 | 0 | const size_t step = n / len; |
132 | 0 | for (size_t i = 0; i < n; i += len) |
133 | 0 | for (size_t k = 0; k < len / 2; ++k) { |
134 | 0 | const double wr = tw[2 * k * step], wi = tw[2 * k * step + 1]; |
135 | 0 | const size_t a = i + k, b = a + len / 2; |
136 | 0 | const double xr = re[b] * wr - im[b] * wi; |
137 | 0 | const double xi = re[b] * wi + im[b] * wr; |
138 | 0 | re[b] = re[a] - xr; im[b] = im[a] - xi; |
139 | 0 | re[a] += xr; im[a] += xi; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | for (size_t i = 0; i < n; ++i) { |
143 | 0 | out[i] = (faar_sample_t)(re[i] * scale); |
144 | 0 | out[n + i] = (faar_sample_t)(im[i] * scale); |
145 | 0 | } |
146 | 0 | FreeMemory(re); |
147 | 0 | return 0; |
148 | 0 | } |
149 | | |
150 | | /* Float-sample counterpart of init_compensated_spectrum; the stored spectrum is |
151 | | * float but computed in double. double_spectrum is radix-2 only. */ |
152 | 0 | static int init_spectrum_rounded(faar_dft_fir *stage, const double *taps) { |
153 | 0 | const size_t n = stage->fft_size; |
154 | 0 | if (n < 2 || (n & (n - 1)) || n > (size_t)INT_MAX) |
155 | 0 | return -1; |
156 | 0 | const double scale = 1.0 / ((double)(n / stage->decimation) * (double)stage->decimation); |
157 | 0 | faar_sample_t *spectrum = (faar_sample_t *)AllocMemory(2 * n * sizeof(*spectrum)); |
158 | 0 | if (!spectrum) return -1; |
159 | 0 | if (double_spectrum(spectrum, taps, stage->tap_count, n, scale)) { |
160 | 0 | FreeMemory(spectrum); |
161 | 0 | return -1; |
162 | 0 | } |
163 | 0 | stage->spec = spectrum; |
164 | 0 | return init_fft_plans(stage); |
165 | 0 | } |
166 | | |
167 | 0 | static int init_paired(faar_dft_fir *stage, const double *taps) { |
168 | 0 | if (stage->compensated) |
169 | 0 | return init_compensated_spectrum(stage, taps) || init_paired_buffers(stage) ? -1 : 0; |
170 | 0 | return init_spectrum_rounded(stage, taps) || init_paired_buffers(stage) ? -1 : 0; |
171 | 0 | } |
172 | | |
173 | | static int init_geometry(faar_dft_fir *stage, size_t tap_count, |
174 | | size_t fft_size, size_t expansion, size_t decimation, |
175 | 0 | size_t lanes, int stacked) { |
176 | 0 | if (!stage || !tap_count || tap_count > fft_size |
177 | 0 | || fft_size > SIZE_MAX / (2 * sizeof(faar_sample_t)) |
178 | 0 | || (expansion != 1 && expansion != 2) |
179 | 0 | || (decimation != 1 && decimation != 2 && decimation != 4) |
180 | 0 | || (fft_size - tap_count + 1) % expansion |
181 | 0 | || (tap_count - 1) % expansion |
182 | 0 | || (fft_size - tap_count + 1) % decimation |
183 | 0 | || (tap_count - 1) % decimation) |
184 | 0 | return -1; |
185 | 0 | memset(stage, 0, sizeof(*stage)); |
186 | 0 | stage->lanes = lanes; |
187 | 0 | stage->fft_size = fft_size; |
188 | 0 | stage->tap_count = tap_count; |
189 | 0 | stage->expansion = expansion; |
190 | 0 | stage->decimation = decimation; |
191 | 0 | stage->block_size = fft_size - tap_count + 1; |
192 | 0 | stage->output_block_size = stage->block_size / decimation; |
193 | 0 | stage->input_block_size = stage->block_size / expansion; |
194 | 0 | stage->stacked = stacked; |
195 | 0 | stage->stream_in = stage->input_block_size * (stacked ? 2 : 1); |
196 | 0 | stage->stream_out = stage->output_block_size * (stacked ? 2 : 1); |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | | static int init_stage(faar_dft_fir *stage, const double *taps, |
201 | | size_t tap_count, size_t fft_size, size_t expansion, |
202 | 0 | size_t decimation, size_t lanes, int stacked, int compensated) { |
203 | 0 | if (!taps || (lanes != 1 && lanes != 2) || (stacked && lanes != 1) |
204 | 0 | || (expansion && fft_size / expansion > (size_t)INT_MAX) |
205 | 0 | || init_geometry(stage, tap_count, fft_size, expansion, |
206 | 0 | decimation, lanes, stacked)) |
207 | 0 | return -1; |
208 | 0 | stage->compensated = compensated; |
209 | 0 | if (init_paired(stage, taps)) { |
210 | 0 | faar_dft_fir_clear(stage); |
211 | 0 | return -1; |
212 | 0 | } |
213 | 0 | return 0; |
214 | 0 | } |
215 | | |
216 | 0 | static int init_halfband_buffers(faar_dft_fir *stage) { |
217 | 0 | if (stage->compensated) { |
218 | 0 | stage->half_work = faar_alloc_zeroed(stage->block_size / 2, sizeof(*stage->half_work)); |
219 | 0 | if (!stage->half_work) return -1; |
220 | 0 | } |
221 | 0 | stage->work = (faar_sample_t *)faar_alloc_zeroed(stage->fft_size + 2, sizeof(faar_sample_t)); |
222 | 0 | stage->overlap = (faar_sample_t *)faar_alloc_zeroed(stage->tap_count - 1, sizeof(faar_sample_t)); |
223 | 0 | stage->input_block = (faar_sample_t *)faar_alloc_zeroed(stage->input_block_size, |
224 | 0 | sizeof(faar_sample_t)); |
225 | 0 | stage->output_block = (faar_sample_t *)faar_alloc_zeroed(stage->block_size, sizeof(faar_sample_t)); |
226 | 0 | return stage->work && stage->overlap && stage->input_block |
227 | 0 | && stage->output_block ? 0 : -1; |
228 | 0 | } |
229 | | |
230 | | static int init_halfband(faar_dft_fir *stage, const double *taps, |
231 | 0 | size_t tap_count, int compensated) { |
232 | 0 | if (!taps || tap_count < 5 || (tap_count - 1) % 4 |
233 | 0 | || tap_count > SIZE_MAX - FAAR_HALFBAND_BLOCK |
234 | 0 | || init_geometry(stage, tap_count, |
235 | 0 | FAAR_HALFBAND_BLOCK + tap_count - 1, 1, 2, 1, 0)) |
236 | 0 | return -1; |
237 | 0 | stage->halfband = 1; |
238 | 0 | stage->compensated = compensated; |
239 | 0 | if (compensated) { |
240 | 0 | stage->half_coeffs = AllocMemory((tap_count / 4 + 1) * sizeof(*stage->half_coeffs)); |
241 | 0 | if (!stage->half_coeffs) { faar_dft_fir_clear(stage); return -1; } |
242 | 0 | for (size_t j = 0; j < tap_count / 4; ++j) |
243 | 0 | stage->half_coeffs[j] = faar_compensated_setup(taps[2 * j + 1]); |
244 | 0 | stage->half_coeffs[tap_count / 4] = faar_compensated_setup(taps[tap_count / 2]); |
245 | 0 | } |
246 | | /* Odd taps and the center: the even non-center taps are zero. */ |
247 | 0 | stage->filter_spectrum = (faar_sample_t *)faar_alloc_zeroed(tap_count / 4 + 1, |
248 | 0 | sizeof(faar_sample_t)); |
249 | 0 | if (!stage->filter_spectrum || init_halfband_buffers(stage)) { |
250 | 0 | faar_dft_fir_clear(stage); |
251 | 0 | return -1; |
252 | 0 | } |
253 | 0 | for (size_t j = 0; j < tap_count / 4; ++j) |
254 | 0 | stage->filter_spectrum[j] = taps[2 * j + 1]; |
255 | 0 | stage->filter_spectrum[tap_count / 4] = taps[tap_count / 2]; |
256 | 0 | return 0; |
257 | 0 | } |
258 | | |
259 | | int faar_dft_fir_init_halfband(faar_dft_fir *stage, const double *taps, |
260 | 0 | size_t tap_count) { |
261 | 0 | return init_halfband(stage, taps, tap_count, 0); |
262 | 0 | } |
263 | | |
264 | | int faar_dft_fir_init_compensated(faar_dft_fir *stage, const double *taps, |
265 | | size_t tap_count, size_t fft_size, size_t expansion, |
266 | 0 | size_t decimation, size_t lanes, int stacked, int halfband) { |
267 | 0 | return halfband ? init_halfband(stage, taps, tap_count, 1) |
268 | 0 | : init_stage(stage, taps, tap_count, fft_size, expansion, decimation, |
269 | 0 | lanes, stacked, 1); |
270 | 0 | } |
271 | | |
272 | | static int init_shared_internal(faar_dft_fir *stage, |
273 | | const faar_dft_fir *source, size_t lanes, int stacked, |
274 | 0 | int private_workspace) { |
275 | 0 | if (!stage || !source || stage == source |
276 | 0 | || (lanes != 1 && lanes != 2) |
277 | 0 | || (stacked != 0 && stacked != 1) || (stacked && lanes != 1) |
278 | 0 | || (source->halfband && (lanes != 1 || stacked)) |
279 | 0 | || !(source->halfband ? source->filter_spectrum : paired_spectrum(source)) |
280 | 0 | || init_geometry(stage, source->tap_count, source->fft_size, |
281 | 0 | source->expansion, source->decimation, lanes, stacked)) |
282 | 0 | return -1; |
283 | 0 | stage->shared_coeffs = 1; |
284 | 0 | stage->halfband = source->halfband; |
285 | 0 | stage->compensated = source->compensated; |
286 | | #if FAAR_RESAMPLER_FLOAT |
287 | | stage->spec_low = source->spec_low; |
288 | | #else |
289 | 0 | stage->spec_wide = source->spec_wide; |
290 | 0 | #endif |
291 | 0 | stage->half_coeffs = source->half_coeffs; |
292 | 0 | stage->filter_spectrum = source->filter_spectrum; |
293 | 0 | stage->spec = source->spec; |
294 | | /* Ordinary channel stages execute sequentially and borrow FFT work. |
295 | | * Reusable filter clones instead build independent execution plans. */ |
296 | 0 | stage->shared_fft = !stage->halfband && !private_workspace; |
297 | 0 | if (stage->shared_fft) { |
298 | 0 | stage->in = source->in; |
299 | 0 | stage->out = source->out; |
300 | | #if FAAR_RESAMPLER_FLOAT |
301 | | stage->precise_in = source->precise_in; |
302 | | stage->precise_out = source->precise_out; |
303 | | #else |
304 | 0 | stage->in_wide = source->in_wide; |
305 | 0 | stage->out_wide = source->out_wide; |
306 | 0 | #endif |
307 | 0 | } |
308 | 0 | int bad; |
309 | 0 | if (stage->halfband) { |
310 | 0 | bad = init_halfband_buffers(stage); |
311 | 0 | } else if (private_workspace) { |
312 | 0 | int plan_bad; |
313 | | #if FAAR_RESAMPLER_FLOAT |
314 | | if (stage->compensated) |
315 | | plan_bad = init_precise_plans(stage, source); |
316 | | else |
317 | | #else |
318 | 0 | if (stage->compensated) |
319 | 0 | plan_bad = init_fft_plans_filter_shared_wide(stage, source); |
320 | 0 | else |
321 | 0 | #endif |
322 | 0 | plan_bad = init_fft_plans_filter_shared(stage, source); |
323 | 0 | bad = plan_bad || init_paired_buffers(stage); |
324 | 0 | } else { |
325 | 0 | bad = init_paired_buffers(stage); |
326 | 0 | } |
327 | 0 | if (bad) { |
328 | 0 | faar_dft_fir_clear(stage); |
329 | 0 | return -1; |
330 | 0 | } |
331 | 0 | return 0; |
332 | 0 | } |
333 | | |
334 | | int faar_dft_fir_init_shared(faar_dft_fir *stage, |
335 | 0 | const faar_dft_fir *source, size_t lanes, int stacked) { |
336 | 0 | return init_shared_internal(stage, source, lanes, stacked, 0); |
337 | 0 | } |
338 | | |
339 | | int faar_dft_fir_init_filter_shared(faar_dft_fir *stage, |
340 | 0 | const faar_dft_fir *source, size_t lanes, int stacked) { |
341 | 0 | return init_shared_internal(stage, source, lanes, stacked, 1); |
342 | 0 | } |
343 | | |
344 | 0 | void faar_dft_fir_release_workspace(faar_dft_fir *stage) { |
345 | 0 | if (!stage) return; |
346 | 0 | FreeMemory(stage->work); |
347 | 0 | stage->work = NULL; |
348 | 0 | FreeMemory(stage->half_work); |
349 | 0 | stage->half_work = NULL; |
350 | | #if FAAR_RESAMPLER_FLOAT |
351 | | if (stage->shared_fft) { |
352 | | stage->precise_in.work = NULL; |
353 | | stage->precise_out.work = NULL; |
354 | | } else { |
355 | | FreeMemory(stage->precise_in.work); |
356 | | FreeMemory(stage->precise_out.work); |
357 | | stage->precise_in.work = NULL; |
358 | | stage->precise_out.work = NULL; |
359 | | } |
360 | | #else |
361 | 0 | if (stage->shared_fft) { |
362 | 0 | stage->in_wide.work = NULL; |
363 | 0 | stage->in_wide.scratch = NULL; |
364 | 0 | stage->out_wide.work = NULL; |
365 | 0 | stage->out_wide.scratch = NULL; |
366 | 0 | } else { |
367 | 0 | FreeMemory(stage->in_wide.work); |
368 | 0 | FreeMemory(stage->out_wide.work); |
369 | 0 | stage->in_wide.work = NULL; |
370 | 0 | stage->in_wide.scratch = NULL; |
371 | 0 | stage->out_wide.work = NULL; |
372 | 0 | stage->out_wide.scratch = NULL; |
373 | 0 | } |
374 | 0 | #endif |
375 | 0 | if (stage->shared_fft) { |
376 | 0 | stage->in.work = NULL; |
377 | 0 | stage->in.scratch = NULL; |
378 | 0 | stage->out.work = NULL; |
379 | 0 | stage->out.scratch = NULL; |
380 | 0 | } else { |
381 | 0 | FreeMemory(stage->in.work); |
382 | 0 | FreeMemory(stage->out.work); |
383 | 0 | stage->in.work = NULL; |
384 | 0 | stage->in.scratch = NULL; |
385 | 0 | stage->out.work = NULL; |
386 | 0 | stage->out.scratch = NULL; |
387 | 0 | } |
388 | 0 | FreeMemory(stage->overlap); |
389 | 0 | FreeMemory(stage->input_block); |
390 | 0 | FreeMemory(stage->output_block); |
391 | 0 | stage->overlap = NULL; |
392 | 0 | stage->input_block = NULL; |
393 | 0 | stage->output_block = NULL; |
394 | 0 | stage->input_fill = stage->output_count = stage->output_offset = 0; |
395 | 0 | stage->finishing = stage->finished = 0; |
396 | 0 | } |
397 | | |
398 | 0 | void faar_dft_fir_prepare(faar_dft_fir *stage) { |
399 | 0 | if (!stage) return; |
400 | 0 | if (stage->halfband) { |
401 | 0 | memset(stage->work, 0, (stage->fft_size + 2) * sizeof(faar_sample_t)); |
402 | 0 | if (stage->half_work) |
403 | 0 | memset(stage->half_work, 0, |
404 | 0 | stage->block_size / 2 * sizeof(*stage->half_work)); |
405 | 0 | } |
406 | | #if FAAR_RESAMPLER_FLOAT |
407 | | else if (stage->compensated) { |
408 | | faar_float2_prepare(&stage->precise_in); |
409 | | faar_float2_prepare(&stage->precise_out); |
410 | | } |
411 | | #else |
412 | 0 | else if (stage->compensated) { |
413 | 0 | faar_fftd_prepare(&stage->in_wide, !stage->out_wide.size); |
414 | 0 | faar_fftd_prepare(&stage->out_wide, 0); |
415 | 0 | } |
416 | 0 | #endif |
417 | 0 | else { |
418 | 0 | faar_fft_prepare(&stage->in, !stage->out.size); |
419 | 0 | faar_fft_prepare(&stage->out, 0); |
420 | 0 | } |
421 | 0 | memset(stage->overlap, 0, stage->lanes * overlap_stride(stage) * sizeof(faar_sample_t)); |
422 | 0 | memset(stage->input_block, 0, stage->lanes * stage->stream_in * sizeof(faar_sample_t)); |
423 | 0 | size_t output = stage->halfband ? stage->block_size : stage->lanes * stage->stream_out; |
424 | 0 | memset(stage->output_block, 0, output * sizeof(faar_sample_t)); |
425 | 0 | } |
426 | | |
427 | 0 | void faar_dft_fir_clear(faar_dft_fir *stage) { |
428 | 0 | if (!stage) return; |
429 | 0 | if (!stage->shared_coeffs) { |
430 | 0 | FreeMemory(stage->filter_spectrum); |
431 | 0 | FreeMemory(stage->spec); |
432 | | #if FAAR_RESAMPLER_FLOAT |
433 | | FreeMemory(stage->spec_low); |
434 | | #else |
435 | 0 | FreeMemory(stage->spec_wide); |
436 | 0 | #endif |
437 | 0 | FreeMemory(stage->half_coeffs); |
438 | 0 | } |
439 | 0 | FreeMemory(stage->work); |
440 | 0 | FreeMemory(stage->overlap); |
441 | 0 | FreeMemory(stage->input_block); |
442 | 0 | FreeMemory(stage->output_block); |
443 | 0 | FreeMemory(stage->half_work); |
444 | | #if FAAR_RESAMPLER_FLOAT |
445 | | if (!stage->shared_fft) { |
446 | | faar_float2_clear(&stage->precise_in); |
447 | | faar_float2_clear(&stage->precise_out); |
448 | | } |
449 | | #else |
450 | 0 | if (!stage->shared_fft) { |
451 | 0 | faar_fftd_clear(&stage->in_wide); |
452 | 0 | faar_fftd_clear(&stage->out_wide); |
453 | 0 | } |
454 | 0 | #endif |
455 | 0 | if (!stage->shared_fft) { |
456 | 0 | faar_fft_clear(&stage->in); |
457 | 0 | faar_fft_clear(&stage->out); |
458 | 0 | } |
459 | 0 | memset(stage, 0, sizeof(*stage)); |
460 | 0 | } |
461 | | |
462 | | |
463 | 0 | static void update_overlap(faar_dft_fir *stage, const faar_sample_t *input) { |
464 | 0 | const size_t overlap = stage->tap_count - 1; |
465 | 0 | if (!overlap) return; |
466 | 0 | for (size_t lane = 0; lane < stage->lanes; ++lane) { |
467 | 0 | const size_t block = stage->stream_in; |
468 | 0 | const faar_sample_t *in = input + lane * block; |
469 | 0 | faar_sample_t *keep = stage->overlap + lane * overlap_stride(stage); |
470 | 0 | size_t stored_overlap = overlap / stage->expansion; |
471 | 0 | if (block >= stored_overlap) { |
472 | 0 | memcpy(keep, in + block - stored_overlap, |
473 | 0 | stored_overlap * sizeof(faar_sample_t)); |
474 | 0 | } else { |
475 | 0 | memmove(keep, keep + block, |
476 | 0 | (stored_overlap - block) * sizeof(faar_sample_t)); |
477 | 0 | memcpy(keep + stored_overlap - block, in, block * sizeof(faar_sample_t)); |
478 | 0 | } |
479 | 0 | } |
480 | 0 | } |
481 | | |
482 | | int faar_dft_fir_process(faar_dft_fir *stage, const faar_sample_t *input, |
483 | 0 | faar_sample_t *output) { |
484 | 0 | if (stage && stage->halfband && input && output) { |
485 | 0 | const size_t n = stage->tap_count; |
486 | 0 | const size_t block = stage->block_size; |
487 | 0 | memcpy(stage->work, stage->overlap, (n - 1) * sizeof(faar_sample_t)); |
488 | 0 | memcpy(stage->work + n - 1, input, block * sizeof(faar_sample_t)); |
489 | 0 | if (stage->compensated) { |
490 | 0 | process_compensated_halfband(stage, output); |
491 | 0 | memcpy(stage->overlap, stage->work + block, (n - 1) * sizeof(faar_sample_t)); |
492 | 0 | return 0; |
493 | 0 | } |
494 | 0 | for (size_t i = 0; i < block / 2; ++i) |
495 | 0 | output[i] = stage->filter_spectrum[n / 4] * stage->work[2 * i + n / 2]; |
496 | | /* Vectorize across independent outputs, without reassociating a |
497 | | * floating-point reduction or requiring compiler-specific flags. */ |
498 | 0 | for (size_t j = 0; j < n / 4; ++j) { |
499 | 0 | const faar_sample_t coefficient = stage->filter_spectrum[j]; |
500 | 0 | const faar_sample_t *x = stage->work + 2 * j + 1; |
501 | 0 | const faar_sample_t *y = stage->work + n - 2 - 2 * j; |
502 | 0 | for (size_t i = 0; i < block / 2; ++i) |
503 | 0 | output[i] += coefficient * (x[2 * i] + y[2 * i]); |
504 | 0 | } |
505 | 0 | memcpy(stage->overlap, stage->work + block, (n - 1) * sizeof(faar_sample_t)); |
506 | 0 | return 0; |
507 | 0 | } |
508 | 0 | if (!stage || !input || !output || !stage->overlap || !paired_spectrum(stage)) |
509 | 0 | return -1; |
510 | | #if FAAR_RESAMPLER_FLOAT |
511 | | if (stage->compensated) convolve_precise(stage, input, output); |
512 | | else |
513 | | #else |
514 | 0 | if (stage->compensated) convolve_paired_wide(stage, input, output); |
515 | 0 | else |
516 | 0 | #endif |
517 | 0 | convolve_paired(stage, input, output); |
518 | 0 | update_overlap(stage, input); |
519 | 0 | return 0; |
520 | 0 | } |
521 | | |
522 | | /* Lane l of the caller's output starts l * stride samples in. */ |
523 | | static size_t stream_drain(faar_dft_fir *stage, faar_sample_t *output, size_t stride, |
524 | 0 | size_t capacity) { |
525 | 0 | size_t remaining = stage->output_count - stage->output_offset; |
526 | 0 | size_t count = remaining < capacity ? remaining : capacity; |
527 | 0 | if (count) { |
528 | 0 | for (size_t lane = 0; lane < stage->lanes; ++lane) |
529 | 0 | memcpy(output + lane * stride, |
530 | 0 | stage->output_block + lane * stage->stream_out |
531 | 0 | + stage->output_offset, count * sizeof(faar_sample_t)); |
532 | 0 | stage->output_offset += count; |
533 | 0 | } |
534 | 0 | if (stage->output_offset == stage->output_count) { |
535 | 0 | stage->output_offset = 0; |
536 | 0 | stage->output_count = 0; |
537 | 0 | } |
538 | 0 | return count; |
539 | 0 | } |
540 | | |
541 | | |
542 | | int faar_dft_fir_stream(faar_dft_fir *stage, const faar_sample_t *input, |
543 | | size_t input_count, size_t *consumed, |
544 | | faar_sample_t *output, size_t output_capacity, |
545 | 0 | size_t *produced) { |
546 | 0 | if (!stage || !consumed || !produced || (input_count && !input) |
547 | 0 | || (output_capacity && !output) || stage->finishing |
548 | 0 | || stage->finished) return -1; |
549 | 0 | *consumed = 0; |
550 | 0 | *produced = 0; |
551 | 0 | *produced += stream_drain(stage, output, output_capacity, |
552 | 0 | output_capacity); |
553 | 0 | while (*consumed < input_count) { |
554 | 0 | if (stage->output_count) break; |
555 | 0 | size_t available = stage->stream_in - stage->input_fill; |
556 | 0 | size_t count = input_count - *consumed; |
557 | 0 | if (count > available) count = available; |
558 | 0 | for (size_t lane = 0; lane < stage->lanes; ++lane) |
559 | 0 | memcpy(stage->input_block + lane * stage->stream_in |
560 | 0 | + stage->input_fill, |
561 | 0 | input + lane * input_count + *consumed, |
562 | 0 | count * sizeof(faar_sample_t)); |
563 | 0 | stage->input_fill += count; |
564 | 0 | *consumed += count; |
565 | 0 | if (stage->input_fill == stage->stream_in) { |
566 | 0 | if (faar_dft_fir_process(stage, stage->input_block, |
567 | 0 | stage->output_block)) return -1; |
568 | 0 | stage->input_fill = 0; |
569 | 0 | stage->output_count = stage->stream_out; |
570 | 0 | *produced += stream_drain(stage, |
571 | 0 | output ? output + *produced : NULL, output_capacity, |
572 | 0 | output_capacity - *produced); |
573 | 0 | } |
574 | 0 | } |
575 | 0 | return 0; |
576 | 0 | } |
577 | | |
578 | | int faar_dft_fir_flush(faar_dft_fir *stage, faar_sample_t *output, |
579 | 0 | size_t output_capacity, size_t *produced) { |
580 | 0 | if (!stage || !produced || (output_capacity && !output)) |
581 | 0 | return -1; |
582 | 0 | *produced = 0; |
583 | 0 | if (stage->finished) return 0; |
584 | 0 | stage->finishing = 1; |
585 | 0 | *produced += stream_drain(stage, output, output_capacity, |
586 | 0 | output_capacity); |
587 | 0 | if (!stage->output_count && stage->input_fill) { |
588 | 0 | const size_t real_count = stage->input_fill; |
589 | 0 | for (size_t lane = 0; lane < stage->lanes; ++lane) |
590 | 0 | memset(stage->input_block + lane * stage->stream_in |
591 | 0 | + real_count, 0, |
592 | 0 | (stage->stream_in - real_count) * sizeof(faar_sample_t)); |
593 | 0 | if (faar_dft_fir_process(stage, stage->input_block, |
594 | 0 | stage->output_block)) return -1; |
595 | 0 | stage->input_fill = 0; |
596 | 0 | stage->output_offset = 0; |
597 | 0 | stage->output_count = (real_count * stage->expansion |
598 | 0 | + stage->decimation - 1) / stage->decimation; |
599 | 0 | *produced += stream_drain(stage, |
600 | 0 | output ? output + *produced : NULL, output_capacity, |
601 | 0 | output_capacity - *produced); |
602 | 0 | } |
603 | 0 | if (!stage->input_fill && !stage->output_count) stage->finished = 1; |
604 | 0 | return 0; |
605 | 0 | } |