/src/faac/libfaar/dft_fir.h
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 | | #ifndef FAAR_DFT_FIR_H |
16 | | #define FAAR_DFT_FIR_H |
17 | | |
18 | | #include "fft.h" |
19 | | #include "libfaar_internal.h" |
20 | | #if FAAR_RESAMPLER_FLOAT |
21 | | #include "fft_float2.h" |
22 | | #endif |
23 | | |
24 | | #include <stddef.h> |
25 | | |
26 | | /* Element type of the compensated halfband's coefficients and accumulator. */ |
27 | | #if FAAR_RESAMPLER_FLOAT |
28 | | typedef faar_float2 faar_compensated_t; |
29 | | static inline faar_compensated_t faar_compensated_setup(double a) { return ff2_setup(a); } |
30 | | #else |
31 | | typedef double faar_compensated_t; |
32 | 1.28k | static inline faar_compensated_t faar_compensated_setup(double a) { return a; }Unexecuted instantiation: dft_prefilter.c:faar_compensated_setup dft_fir.c:faar_compensated_setup Line | Count | Source | 32 | 1.28k | static inline faar_compensated_t faar_compensated_setup(double a) { return a; } |
|
33 | | #endif |
34 | | |
35 | | /* Block overlap-save FIR stage; filter coefficients are supplied by caller. */ |
36 | | typedef struct faar_dft_fir { |
37 | | int shared_coeffs; /* Borrowed immutable coefficients; source outlives stage. */ |
38 | | int shared_fft; /* Borrowed workspace; executions with source are serialized. */ |
39 | | int compensated; /* High: sensitive stages run in wider arithmetic (float2 or double). */ |
40 | | int halfband; /* Sparse time-domain 2:1 FIR; otherwise paired FFT. */ |
41 | | size_t fft_size; |
42 | | size_t tap_count; |
43 | | size_t expansion; |
44 | | size_t block_size; |
45 | | size_t output_block_size; |
46 | | size_t input_block_size; |
47 | | size_t decimation; |
48 | | faar_sample_t *filter_spectrum; |
49 | | faar_sample_t *work; |
50 | | faar_sample_t *overlap; |
51 | | faar_sample_t *input_block; |
52 | | faar_sample_t *output_block; |
53 | | int stacked; |
54 | | size_t stream_in; /* samples per lane the stage takes before it runs */ |
55 | | size_t stream_out; |
56 | | size_t lanes; |
57 | | faar_fft_plan in, out; |
58 | | #if FAAR_RESAMPLER_FLOAT |
59 | | faar_float2_plan precise_in, precise_out; |
60 | | float *spec_low; |
61 | | #else |
62 | | faar_fftd_plan in_wide, out_wide; |
63 | | double *spec_wide; |
64 | | #endif |
65 | | faar_compensated_t *half_coeffs, *half_work; |
66 | | faar_sample_t *spec; |
67 | | size_t input_fill; |
68 | | size_t output_count; |
69 | | size_t output_offset; |
70 | | int finishing; |
71 | | int finished; |
72 | | } faar_dft_fir; |
73 | | |
74 | | /* Input samples per half-band block. */ |
75 | 3.89k | #define FAAR_HALFBAND_BLOCK 256 |
76 | | |
77 | | /* Paired stage: `lanes` (1 or 2) channels share one complex FFT as |
78 | | * z = a + jb, so the two real outputs come out as real and imaginary parts. |
79 | | * With stacked, a lone channel instead pairs each block with the next, so the |
80 | | * stage runs once per two blocks (stream_in input samples). expansion |
81 | | * zero-inserts the input and decimation keeps every decimation-th output; |
82 | | * fft_size must be at least tap_count and a power of two the complex FFT |
83 | | * supports. Lane planes are laid out one after another: input lane l |
84 | | * starts at input + l * input_count, output lane l at output + l * |
85 | | * output_capacity (process: stream_in and stream_out). */ |
86 | | int faar_dft_fir_init_paired(faar_dft_fir *stage, const double *taps, |
87 | | size_t tap_count, size_t fft_size, size_t expansion, |
88 | | size_t decimation, size_t lanes, int stacked); |
89 | | /* Initialize private streaming state using source's filter and FFT workspace. |
90 | | * Executions must be serialized; source must outlive this stage. This is for channel |
91 | | * stages owned by one prefilter, not for sharing across public handles. */ |
92 | | int faar_dft_fir_init_shared(faar_dft_fir *stage, |
93 | | const faar_dft_fir *source, size_t lanes, int stacked); |
94 | | /* Share immutable filter data while allocating private transform workspace. |
95 | | * The source must outlive the stage; per-stage execution needs no serialization. */ |
96 | | int faar_dft_fir_init_filter_shared(faar_dft_fir *stage, |
97 | | const faar_dft_fir *source, size_t lanes, int stacked); |
98 | | /* Sparse time-domain 2:1 stage: N = 4m+1 taps with the even non-center |
99 | | * taps zero. */ |
100 | | int faar_dft_fir_init_halfband(faar_dft_fir *stage, const double *taps, |
101 | | size_t tap_count); |
102 | | /* Float-expansion variant for High. Internal arithmetic remains float. */ |
103 | | int faar_dft_fir_init_compensated(faar_dft_fir *stage, const double *taps, |
104 | | size_t tap_count, size_t fft_size, size_t expansion, |
105 | | size_t decimation, size_t lanes, int stacked, int halfband); |
106 | | void faar_dft_fir_clear(faar_dft_fir *stage); |
107 | | /* Drop mutable execution storage but retain filter and twiddle metadata for |
108 | | * constructing another private-workspace clone. Safe to call more than once. */ |
109 | | void faar_dft_fir_release_workspace(faar_dft_fir *stage); |
110 | | /* Touch private work buffers before the first input. */ |
111 | | void faar_dft_fir_prepare(faar_dft_fir *stage); |
112 | | |
113 | | /* Consumes and produces exactly stage->block_size mono samples per call. */ |
114 | | int faar_dft_fir_process(faar_dft_fir *stage, const faar_sample_t *input, |
115 | | faar_sample_t *output); |
116 | | |
117 | | /* Stream arbitrary chunks. consumed/produced are always set. Output capacity |
118 | | * may be smaller than a transform block; remaining output is retained. */ |
119 | | int faar_dft_fir_stream(faar_dft_fir *stage, const faar_sample_t *input, |
120 | | size_t input_count, size_t *consumed, |
121 | | faar_sample_t *output, size_t output_capacity, |
122 | | size_t *produced); |
123 | | |
124 | | /* Finish a stream, emitting the remaining real input samples. Call until 0. */ |
125 | | int faar_dft_fir_flush(faar_dft_fir *stage, faar_sample_t *output, |
126 | | size_t output_capacity, size_t *produced); |
127 | | |
128 | | #endif |