fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Théo Monnomsoxr-sys (#438)2e4d385

main
7.4 KiB314 linesraw
1/* SoX Resampler Library      Copyright (c) 2007-18 robs@users.sourceforge.net
2 * Licence for this file: LGPL v2.1                  See LICENCE for details.
3 *
4 * Constant-rate resampling engine-specific code. */
5
6#include <math.h>
7#include <assert.h>
8#include <string.h>
9#include <stdlib.h>
10
11#include "filter.h"
12
13#if defined SOXR_LIB
14  #include "internal.h"
15  #include "cr.h"
16  #if CORE_TYPE & CORE_DBL
17    typedef double sample_t;
18    #if CORE_TYPE & CORE_SIMD_DFT
19      #define RDFT_CB    _soxr_rdft64s_cb
20    #else
21      #define RDFT_CB    _soxr_rdft64_cb
22    #endif
23  #else
24    typedef float sample_t;
25    #if CORE_TYPE & CORE_SIMD_DFT
26      #define RDFT_CB    _soxr_rdft32s_cb
27    #else
28      #define RDFT_CB    _soxr_rdft32_cb
29    #endif
30  #endif
31
32  #if CORE_TYPE & (CORE_SIMD_POLY|CORE_SIMD_HALF|CORE_SIMD_DFT)
33    #if CORE_TYPE & CORE_DBL
34      #include "util64s.h"
35      #include "dev64s.h"
36    #else
37      #include "util32s.h"
38      #include "dev32s.h"
39    #endif
40  #endif
41
42  extern fn_t RDFT_CB[];
43#else
44  #define RDFT_CB 0
45#endif
46
47
48
49static void cubic_stage_fn(stage_t * p, fifo_t * output_fifo)
50{
51  sample_t const * input = stage_read_p(p);
52  int num_in = min(stage_occupancy(p), p->input_size);
53  int i, max_num_out = 1 + (int)(num_in * p->out_in_ratio);
54  sample_t * output = fifo_reserve(output_fifo, max_num_out);
55
56  for (i = 0; p->at.integer < num_in; ++i, p->at.whole += p->step.whole) {
57    sample_t const * s = input + p->at.integer;
58    double x = p->at.fraction * (1 / MULT32);
59    double b = .5*(s[1]+s[-1])-*s, a = (1/6.)*(s[2]-s[1]+s[-1]-*s-4*b);
60    double c = s[1]-*s-a-b;
61    output[i] = (sample_t)(p->mult * (((a*x + b)*x + c)*x + *s));
62  }
63  assert(max_num_out - i >= 0);
64  fifo_trim_by(output_fifo, max_num_out - i);
65  fifo_read(&p->fifo, p->at.integer, NULL);
66  p->at.integer = 0;
67}
68
69
70
71#if defined __AVX__
72  #define DEFINED_AVX 1
73#else
74  #define DEFINED_AVX 0
75#endif
76
77#if defined __x86_64__ || defined _M_X64 || defined i386 || defined _M_IX86
78  #define DEFINED_X86 1
79#else
80  #define DEFINED_X86 0
81#endif
82
83#if defined __arm__
84  #define DEFINED_ARM 1
85#else
86  #define DEFINED_ARM 0
87#endif
88
89
90
91#if CORE_TYPE & CORE_DBL
92  #define SIMD_AVX ((CORE_TYPE & CORE_SIMD_HALF) && DEFINED_AVX)
93  #define SIMD_SSE 0
94#else
95  #define SIMD_SSE ((CORE_TYPE & CORE_SIMD_HALF) && DEFINED_X86)
96  #define SIMD_AVX 0
97#endif
98
99#define SIMD_NEON ((CORE_TYPE & CORE_SIMD_HALF) && DEFINED_ARM)
100
101
102
103#include "half-coefs.h"
104
105#if !(CORE_TYPE & CORE_SIMD_HALF)
106#define FUNCTION_H h7
107#define CONVOLVE ____ __ _
108#include "half-fir.h"
109#endif
110
111#define FUNCTION_H h8
112#define CONVOLVE ____ ____
113#include "half-fir.h"
114
115#define FUNCTION_H h9
116#define CONVOLVE ____ ____ _
117#include "half-fir.h"
118
119#if CORE_TYPE & CORE_DBL
120  #define FUNCTION_H h10
121  #define CONVOLVE ____ ____ __
122  #include "half-fir.h"
123
124  #define FUNCTION_H h11
125  #define CONVOLVE ____ ____ __ _
126  #include "half-fir.h"
127
128  #define FUNCTION_H h12
129  #define CONVOLVE ____ ____ ____
130  #include "half-fir.h"
131
132  #define FUNCTION_H h13
133  #define CONVOLVE ____ ____ ____ _
134  #include "half-fir.h"
135#endif
136
137static half_fir_info_t const half_firs[] = {
138#if !(CORE_TYPE & CORE_SIMD_HALF)
139  { 7, half_fir_coefs_7 , h7 , 0  , 120.65f},
140#endif
141  { 8, half_fir_coefs_8 , h8 , 0  , 136.51f},
142  { 9, half_fir_coefs_9 , h9 , 0  , 152.32f},
143#if CORE_TYPE & CORE_DBL
144  {10, half_fir_coefs_10, h10, 0  , 168.08f},
145  {11, half_fir_coefs_11, h11, 0  , 183.79f},
146  {12, half_fir_coefs_12, h12, 0  , 199.46f},
147  {13, half_fir_coefs_13, h13, 0  , 215.12f},
148#endif
149};
150
151#undef SIMD_AVX
152#undef SIMD_NEON
153#undef SIMD_SSE
154
155
156
157#if CORE_TYPE & CORE_DBL
158  #define SIMD_AVX ((CORE_TYPE & CORE_SIMD_POLY) && DEFINED_AVX)
159  #define SIMD_SSE 0
160#else
161  #define SIMD_SSE ((CORE_TYPE & CORE_SIMD_POLY) && DEFINED_X86)
162  #define SIMD_AVX 0
163#endif
164
165#define SIMD_NEON ((CORE_TYPE & CORE_SIMD_POLY) && DEFINED_ARM)
166
167
168
169#define COEFS (sample_t * __restrict)p->shared->poly_fir_coefs
170#define VAR_LENGTH p->n
171#define VAR_CONVOLVE(n) while (j < (n)) _
172#define VAR_POLY_PHASE_BITS p->phase_bits
173
174
175
176#define FUNCTION vpoly0
177#define FIR_LENGTH VAR_LENGTH
178#define CONVOLVE(n) VAR_CONVOLVE(n)
179#include "poly-fir0.h"
180
181#define FUNCTION vpoly1
182#define COEF_INTERP 1
183#define PHASE_BITS VAR_POLY_PHASE_BITS
184#define FIR_LENGTH VAR_LENGTH
185#define CONVOLVE(n) VAR_CONVOLVE(n)
186#include "poly-fir.h"
187
188#define FUNCTION vpoly2
189#define COEF_INTERP 2
190#define PHASE_BITS VAR_POLY_PHASE_BITS
191#define FIR_LENGTH VAR_LENGTH
192#define CONVOLVE(n) VAR_CONVOLVE(n)
193#include "poly-fir.h"
194
195#define FUNCTION vpoly3
196#define COEF_INTERP 3
197#define PHASE_BITS VAR_POLY_PHASE_BITS
198#define FIR_LENGTH VAR_LENGTH
199#define CONVOLVE(n) VAR_CONVOLVE(n)
200#include "poly-fir.h"
201
202
203
204#if !(CORE_TYPE & CORE_SIMD_POLY)
205
206#define poly_fir_convolve_U100 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
207#define FUNCTION U100_0
208#define FIR_LENGTH U100_l
209#define CONVOLVE(n) poly_fir_convolve_U100
210#include "poly-fir0.h"
211
212#define u100_l 11
213#define poly_fir_convolve_u100 _ _ _ _ _ _ _ _ _ _ _
214#define FUNCTION u100_0
215#define FIR_LENGTH u100_l
216#define CONVOLVE(n) poly_fir_convolve_u100
217#include "poly-fir0.h"
218
219#define FUNCTION u100_1
220#define COEF_INTERP 1
221#define PHASE_BITS 8
222#define FIR_LENGTH u100_l
223#define CONVOLVE(n) poly_fir_convolve_u100
224#include "poly-fir.h"
225
226#define FUNCTION u100_2
227#define COEF_INTERP 2
228#define PHASE_BITS 6
229#define FIR_LENGTH u100_l
230#define CONVOLVE(n) poly_fir_convolve_u100
231#include "poly-fir.h"
232
233#endif
234
235#define u100_1_b 8
236#define u100_2_b 6
237
238
239
240static poly_fir_t const poly_firs[] = {
241  {-1, {{0, vpoly0}, { 7.2f, vpoly1}, {5.0f, vpoly2}}},
242  {-1, {{0, vpoly0}, { 9.4f, vpoly1}, {6.7f, vpoly2}}},
243  {-1, {{0, vpoly0}, {12.4f, vpoly1}, {7.8f, vpoly2}}},
244  {-1, {{0, vpoly0}, {13.6f, vpoly1}, {9.3f, vpoly2}}},
245  {-1, {{0, vpoly0}, {10.5f, vpoly2}, {8.4f, vpoly3}}},
246  {-1, {{0, vpoly0}, {11.85f,vpoly2}, {9.0f, vpoly3}}},
247
248  {-1, {{0, vpoly0}, { 8.0f, vpoly1}, {5.3f, vpoly2}}},
249  {-1, {{0, vpoly0}, { 8.6f, vpoly1}, {5.7f, vpoly2}}},
250  {-1, {{0, vpoly0}, {10.6f, vpoly1}, {6.75f,vpoly2}}},
251  {-1, {{0, vpoly0}, {12.6f, vpoly1}, {8.6f, vpoly2}}},
252  {-1, {{0, vpoly0}, { 9.6f, vpoly2}, {7.6f, vpoly3}}},
253  {-1, {{0, vpoly0}, {11.4f, vpoly2}, {8.65f,vpoly3}}},
254
255#if CORE_TYPE & CORE_SIMD_POLY
256  {10.62f, {{0, vpoly0}, {0, 0}, {0, 0}}},
257  {-1, {{0, vpoly0}, {u100_1_b, vpoly1}, {u100_2_b, vpoly2}}},
258#else
259  {10.62f, {{U100_l, U100_0}, {0, 0}, {0, 0}}},
260  {11.28f, {{u100_l, u100_0}, {u100_1_b, u100_1}, {u100_2_b, u100_2}}},
261#endif
262  {-1, {{0, vpoly0}, {   9, vpoly1}, {  6, vpoly2}}},
263  {-1, {{0, vpoly0}, {  11, vpoly1}, {  7, vpoly2}}},
264  {-1, {{0, vpoly0}, {  13, vpoly1}, {  8, vpoly2}}},
265  {-1, {{0, vpoly0}, {  10, vpoly2}, {  8, vpoly3}}},
266  {-1, {{0, vpoly0}, {  12, vpoly2}, {  9, vpoly3}}},
267};
268
269
270
271static cr_core_t const cr_core = {
272
273#if CORE_TYPE & CORE_SIMD_POLY
274  {SIMD_ALIGNED_MALLOC, SIMD_ALIGNED_CALLOC, SIMD_ALIGNED_FREE},
275#else
276  {malloc, calloc, free},
277#endif
278  half_firs, array_length(half_firs),
279  0, 0,
280  cubic_stage_fn,
281  poly_firs, RDFT_CB
282};
283
284
285
286#if defined SOXR_LIB
287
288#include "soxr.h"
289
290static char const * rate_create(void * channel, void * shared, double io_ratio,
291    soxr_quality_spec_t * q_spec, soxr_runtime_spec_t * r_spec, double scale)
292{
293  return _soxr_init(channel, shared, io_ratio, q_spec, r_spec, scale,
294      &cr_core, CORE_TYPE);
295}
296
297
298
299static char const * id(void) {return CORE_STR;}
300
301fn_t RATE_CB[] = {
302  (fn_t)_soxr_input,
303  (fn_t)_soxr_process,
304  (fn_t)_soxr_output,
305  (fn_t)_soxr_flush,
306  (fn_t)_soxr_close,
307  (fn_t)_soxr_delay,
308  (fn_t)_soxr_sizes,
309  (fn_t)rate_create,
310  (fn_t)0,
311  (fn_t)id,
312};
313
314#endif