fork/rust-sdks

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

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

livekit/livekit-ffi@0.13.0
21.9 KiB588 linesraw
1/* SoX Resampler Library      Copyright (c) 2007-16 robs@users.sourceforge.net
2 * Licence for this file: LGPL v2.1                  See LICENCE for details.
3 *
4 * Constant-rate resampling common 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  #define STATIC
16#endif
17
18#include "cr.h"
19
20#define num_coefs4 ((core_flags&CORE_SIMD_POLY)? ((num_coefs+3)&~3) : num_coefs)
21
22#define coef_coef(C,T,x) \
23  C((T*)result, interp_order, num_coefs4, j, x, num_coefs4 - 1 - i)
24
25#define STORE(C,T) { \
26  if (interp_order > 2) coef_coef(C,T,3) = (T)d; \
27  if (interp_order > 1) coef_coef(C,T,2) = (T)c; \
28  if (interp_order > 0) coef_coef(C,T,1) = (T)b; \
29  coef_coef(C,T,0) = (T)f0;}
30
31static real * prepare_poly_fir_coefs(double const * coefs, int num_coefs,
32    int num_phases, int interp_order, double multiplier,
33    core_flags_t core_flags, alloc_t const * mem)
34{
35  int i, j, length = num_coefs4 * num_phases * (interp_order + 1);
36  real * result = mem->calloc(1,(size_t)length << LOG2_SIZEOF_REAL(core_flags));
37  double fm1 = coefs[0], f1 = 0, f2 = 0;
38
39  for (i = num_coefs - 1; i >= 0; --i)
40    for (j = num_phases - 1; j >= 0; --j) {
41      double f0 = fm1, b = 0, c = 0, d = 0; /* = 0 to kill compiler warning */
42      int pos = i * num_phases + j - 1;
43      fm1 = pos > 0 ? coefs[pos - 1] * multiplier : 0;
44      switch (interp_order) {
45        case 1: b = f1 - f0; break;
46        case 2: b = f1 - (.5 * (f2+f0) - f1) - f0; c = .5 * (f2+f0) - f1; break;
47        case 3: c=.5*(f1+fm1)-f0;d=(1/6.)*(f2-f1+fm1-f0-4*c);b=f1-f0-d-c; break;
48        default: assert(!interp_order);
49      }
50      switch (core_flags & 3) {
51        case 0: if (WITH_CR32 ) STORE(coef , float ); break;
52        case 1: if (WITH_CR64 ) STORE(coef , double); break;
53        case 2: if (WITH_CR32S) STORE(coef4, float ); break;
54        default:if (WITH_CR64S) STORE(coef4, double); break;
55      }
56      f2 = f1, f1 = f0;
57    }
58  return result;
59}
60
61#undef STORE
62#undef coef_coef
63
64#define IS_FLOAT32 (WITH_CR32 || WITH_CR32S) && \
65    (!(WITH_CR64 || WITH_CR64S) || sizeof_real == sizeof(float))
66#define WITH_FLOAT64 WITH_CR64 || WITH_CR64S
67
68static void dft_stage_fn(stage_t * p, fifo_t * output_fifo)
69{
70  real * output, * dft_out;
71  int i, j, num_in = max(0, fifo_occupancy(&p->fifo));
72  rate_shared_t const * s = p->shared;
73  dft_filter_t const * f = &s->dft_filter[p->dft_filter_num];
74  int const overlap = f->num_taps - 1;
75
76  if (p->at.integer + p->L * num_in >= f->dft_length) {
77    fn_t const * const RDFT_CB = p->rdft_cb;
78    size_t const sizeof_real = sizeof(char) << LOG2_SIZEOF_REAL(p->core_flags);
79    div_t divd = div(f->dft_length - overlap - p->at.integer + p->L - 1, p->L);
80    real const * input = fifo_read_ptr(&p->fifo);
81    fifo_read(&p->fifo, divd.quot, NULL);
82    num_in -= divd.quot;
83
84    output = fifo_reserve(output_fifo, f->dft_length);
85    dft_out = (p->core_flags & CORE_SIMD_DFT)? p->dft_out : output;
86
87    if (lsx_is_power_of_2(p->L)) { /* F-domain */
88      int portion = f->dft_length / p->L;
89      memcpy(dft_out, input, (unsigned)portion * sizeof_real);
90      rdft_oforward(portion, f->dft_forward_setup, dft_out, p->dft_scratch);
91      if (IS_FLOAT32) {
92#define dft_out ((float *)dft_out)
93        for (i = portion + 2; i < (portion << 1); i += 2) /* Mirror image. */
94          dft_out[i] = dft_out[(portion << 1) - i],
95            dft_out[i+1] = -dft_out[(portion << 1) - i + 1];
96        dft_out[portion] = dft_out[1];
97        dft_out[portion + 1] = 0;
98        dft_out[1] = dft_out[0];
99#undef dft_out
100      }
101      else if (WITH_FLOAT64) {
102#define dft_out ((double *)dft_out)
103        for (i = portion + 2; i < (portion << 1); i += 2) /* Mirror image. */
104          dft_out[i] = dft_out[(portion << 1) - i],
105            dft_out[i+1] = -dft_out[(portion << 1) - i + 1];
106        dft_out[portion] = dft_out[1];
107        dft_out[portion + 1] = 0;
108        dft_out[1] = dft_out[0];
109#undef dft_out
110      }
111
112      for (portion <<= 1; i < f->dft_length; i += portion, portion <<= 1) {
113        memcpy((char *)dft_out + (size_t)i * sizeof_real, dft_out, (size_t)portion * sizeof_real);
114        if (IS_FLOAT32)
115        #define dft_out ((float *)dft_out)
116          dft_out[i + 1] = 0;
117        #undef dft_out
118        else if (WITH_FLOAT64)
119        #define dft_out ((double *)dft_out)
120          dft_out[i + 1] = 0;
121        #undef dft_out
122      }
123      if (p->step.integer > 0)
124        rdft_reorder_back(f->dft_length, f->dft_backward_setup, dft_out, p->dft_scratch);
125    } else {
126      if (p->L == 1)
127        memcpy(dft_out, input, (size_t)f->dft_length * sizeof_real);
128      else {
129        memset(dft_out, 0, (size_t)f->dft_length * sizeof_real);
130        if (IS_FLOAT32)
131          for (j = 0, i = p->at.integer; i < f->dft_length; ++j, i += p->L)
132            ((float *)dft_out)[i] = ((float *)input)[j];
133        else if (WITH_FLOAT64)
134          for (j = 0, i = p->at.integer; i < f->dft_length; ++j, i += p->L)
135            ((double *)dft_out)[i] = ((double *)input)[j];
136        p->at.integer = p->L - 1 - divd.rem;
137      }
138      if (p->step.integer > 0)
139        rdft_forward(f->dft_length, f->dft_forward_setup, dft_out, p->dft_scratch);
140      else
141        rdft_oforward(f->dft_length, f->dft_forward_setup, dft_out, p->dft_scratch);
142    }
143
144    if (p->step.integer > 0) {
145      rdft_convolve(f->dft_length, f->dft_backward_setup, dft_out, f->coefs);
146      rdft_backward(f->dft_length, f->dft_backward_setup, dft_out, p->dft_scratch);
147      if ((p->core_flags & CORE_SIMD_DFT) && p->step.integer == 1)
148        memcpy(output, dft_out, (size_t)f->dft_length * sizeof_real);
149      if (p->step.integer != 1) {
150        if (IS_FLOAT32)
151          for (j = 0, i = p->remM; i < f->dft_length - overlap; ++j,
152              i += p->step.integer)
153            ((float *)output)[j] = ((float *)dft_out)[i];
154        else if (WITH_FLOAT64)
155          for (j = 0, i = p->remM; i < f->dft_length - overlap; ++j,
156              i += p->step.integer)
157            ((double *)output)[j] = ((double *)dft_out)[i];
158        p->remM = i - (f->dft_length - overlap);
159        fifo_trim_by(output_fifo, f->dft_length - j);
160      }
161      else fifo_trim_by(output_fifo, overlap);
162    }
163    else { /* F-domain */
164      int m = -p->step.integer;
165      rdft_convolve_portion(f->dft_length >> m, dft_out, f->coefs);
166      rdft_obackward(f->dft_length >> m, f->dft_backward_setup, dft_out, p->dft_scratch);
167      if (p->core_flags & CORE_SIMD_DFT)
168        memcpy(output, dft_out, (size_t)(f->dft_length >> m) * sizeof_real);
169      fifo_trim_by(output_fifo, (((1 << m) - 1) * f->dft_length + overlap) >>m);
170    }
171    (void)RDFT_CB;
172  }
173  p->input_size = (f->dft_length - p->at.integer + p->L - 1) / p->L;
174}
175
176/* Set to 4 x nearest power of 2 or half of that */
177/* if danger of causing too many cache misses. */
178static int set_dft_length(int num_taps, int min, int large)
179{
180  double d = log((double)num_taps) / log(2.);
181  return 1 << range_limit((int)(d + 2.77), min, max((int)(d + 1.77), large));
182}
183
184static void dft_stage_init(
185    unsigned instance, double Fp, double Fs, double Fn, double att,
186    double phase_response, stage_t * p, int L, int M, double * multiplier,
187    unsigned min_dft_size, unsigned large_dft_size, core_flags_t core_flags,
188    fn_t const * RDFT_CB)
189{
190  dft_filter_t * f = &p->shared->dft_filter[instance];
191  int num_taps = 0, dft_length = f->dft_length, i, offset;
192  bool f_domain_m = abs(3-M) == 1 && Fs <= 1;
193  size_t const sizeof_real = sizeof(char) << LOG2_SIZEOF_REAL(core_flags);
194
195  if (!dft_length) {
196    int k = phase_response == 50 && lsx_is_power_of_2(L) && Fn == L? L << 1 : 4;
197    double m, * h = lsx_design_lpf(Fp, Fs, Fn, att, &num_taps, -k, -1.);
198
199    if (phase_response != 50)
200      lsx_fir_to_phase(&h, &num_taps, &f->post_peak, phase_response);
201    else f->post_peak = num_taps / 2;
202
203    dft_length = set_dft_length(num_taps, (int)min_dft_size, (int)large_dft_size);
204    f->coefs = rdft_calloc((size_t)dft_length, sizeof_real);
205    offset = dft_length - num_taps + 1;
206    m = (1. / dft_length) * rdft_multiplier() * L * *multiplier;
207    if (IS_FLOAT32) for (i = 0; i < num_taps; ++i)
208        ((float *)f->coefs)[(i + offset) & (dft_length - 1)] =(float)(h[i] * m);
209    else if (WITH_FLOAT64) for (i = 0; i < num_taps; ++i)
210        ((double *)f->coefs)[(i + offset) & (dft_length - 1)] = h[i] * m;
211    free(h);
212  }
213
214  if (rdft_flags() & RDFT_IS_SIMD)
215    p->dft_out = rdft_malloc(sizeof_real * (size_t)dft_length);
216  if (rdft_flags() & RDFT_NEEDS_SCRATCH)
217    p->dft_scratch = rdft_malloc(2 * sizeof_real * (size_t)dft_length);
218
219  if (!f->dft_length) {
220    void * coef_setup = rdft_forward_setup(dft_length);
221    int Lp = lsx_is_power_of_2(L)? L : 1;
222    int Mp = f_domain_m? M : 1;
223    f->dft_forward_setup = rdft_forward_setup(dft_length / Lp);
224    f->dft_backward_setup = rdft_backward_setup(dft_length / Mp);
225    if (Mp == 1)
226      rdft_forward(dft_length, coef_setup, f->coefs, p->dft_scratch);
227    else
228      rdft_oforward(dft_length, coef_setup, f->coefs, p->dft_scratch);
229    rdft_delete_setup(coef_setup);
230    f->num_taps = num_taps;
231    f->dft_length = dft_length;
232    lsx_debug("fir_len=%i dft_length=%i Fp=%g Fs=%g Fn=%g att=%g %i/%i",
233        num_taps, dft_length, Fp, Fs, Fn, att, L, M);
234  }
235  *multiplier = 1;
236  p->out_in_ratio = (double)L / M;
237  p->core_flags = core_flags;
238  p->rdft_cb = RDFT_CB;
239  p->fn = dft_stage_fn;
240  p->preload = f->post_peak / L;
241  p->at.integer = f->post_peak % L;
242  p->L = L;
243  p->step.integer = f_domain_m? -M/2 : M;
244  p->dft_filter_num = instance;
245  p->block_len = f->dft_length - (f->num_taps - 1);
246  p->phase0 = p->at.integer / p->L;
247  p->input_size = (f->dft_length - p->at.integer + p->L - 1) / p->L;
248}
249
250static struct half_fir_info const * find_half_fir(
251    struct half_fir_info const * firs, size_t len, double att)
252{
253  size_t i;
254  for (i = 0; i + 1 < len && att > firs[i].att; ++i);
255  return &firs[i];
256}
257
258#define have_pre_stage  (preM  * preL  != 1)
259#define have_arb_stage  (arbM  * arbL  != 1)
260#define have_post_stage (postM * postL != 1)
261
262#include "soxr.h"
263
264STATIC char const * _soxr_init(
265  rate_t * const p,             /* Per audio channel. */
266  rate_shared_t * const shared, /* By channels undergoing same rate change. */
267  double const io_ratio,        /* Input rate divided by output rate. */
268  soxr_quality_spec_t const * const q_spec,
269  soxr_runtime_spec_t const * const r_spec,
270  double multiplier,            /* Linear gain to apply during conversion. */
271  cr_core_t const * const core,
272  core_flags_t const core_flags)
273{
274  size_t const sizeof_real = sizeof(char) << LOG2_SIZEOF_REAL(core_flags);
275  double const tolerance = 1 + 1e-5;
276
277  double       bits = q_spec->precision;
278  rolloff_t const rolloff = (rolloff_t)(q_spec->flags & 3);
279  int interpolator = (int)(r_spec->flags & 3) - 1;
280  double const Fp0 = q_spec->passband_end, Fs0 = q_spec->stopband_begin;
281  double const phase_response = q_spec->phase_response, tbw0 = Fs0-Fp0;
282
283  bool const maintain_3dB_pt = !!(q_spec->flags & SOXR_MAINTAIN_3DB_PT);
284  double tbw_tighten = 1, alpha;
285  #define tighten(x) (Fs0-(Fs0-(x))*tbw_tighten)
286
287  double arbM = io_ratio, Fn1, Fp1 = Fp0, Fs1 = Fs0, bits1 = min(bits,33);
288  double att = (bits1 + 1) * linear_to_dB(2.), attArb = att; /* +1: pass+stop */
289  int preL = 1, preM = 1, shr = 0, arbL = 1, postL = 1, postM = 1;
290  bool upsample=false, rational=false, iOpt=!(r_spec->flags&SOXR_NOSMALLINTOPT);
291  bool lq_bits= (q_spec->flags & SOXR_PROMOTE_TO_LQ)? bits <= 16 : bits == 16;
292  bool lq_Fp0 = (q_spec->flags & SOXR_PROMOTE_TO_LQ)? Fp0<=lq_bw0 : Fp0==lq_bw0;
293  int n = 0, i, mode = lq_bits && rolloff == rolloff_medium? io_ratio > 1 ||
294    phase_response != 50 || !lq_Fp0 || Fs0 != 1 : ((int)ceil(bits1) - 6) / 4;
295  struct half_fir_info const * half_fir_info;
296  stage_t * s;
297
298  if (io_ratio < 1 && Fs0 - 1 > 1 - Fp0 / tolerance)
299    return "imaging greater than rolloff";
300  if (.002 / tolerance > tbw0 || tbw0 > .5 * tolerance)
301    return "transition bandwidth not in [0.2,50] % of nyquist";
302  if (.5 / tolerance > Fp0 || Fs0 > 1.5 * tolerance)
303    return "transition band not within [50,150] % of nyquist";
304  if (bits!=0 && (15 > bits || bits > 33))
305    return "precision not in [15,33] bits";
306  if (io_ratio <= 0)
307    return "resampling factor not positive";
308  if (0 > phase_response || phase_response > 100)
309    return "phase response not in [0=min-phase,100=max-phase] %";
310
311  p->core = core;
312  p->io_ratio = io_ratio;
313  if (bits!=0) while (!n++) {                            /* Determine stages: */
314    int try, L, M, x, maxL = interpolator > 0? 1 : mode? 2048 :
315      (int)ceil(r_spec->coef_size_kbytes * 1000. / (U100_l * (int)sizeof_real));
316    double d, epsilon = 0, frac;
317    upsample = arbM < 1;
318    for (i = (int)(.5 * arbM), shr = 0; i >>= 1; arbM *= .5, ++shr);
319    preM = upsample || (arbM > 1.5 && arbM < 2);
320    postM = 1 + (arbM > 1 && preM), arbM /= postM;
321    preL = 1 + (!preM && arbM < 2) + (upsample && mode), arbM *= preL;
322    if ((frac = arbM - (int)arbM)!=0)
323      epsilon = fabs(floor(frac * MULT32 + .5) / (frac * MULT32) - 1);
324    for (i = 1, rational = frac==0; i <= maxL && !rational; ++i) {
325      d = frac * i, try = (int)(d + .5);
326      if ((rational = fabs(try / d - 1) <= epsilon)) {    /* No long doubles! */
327        if (try == i)
328          arbM = ceil(arbM), shr += x = arbM > 3, arbM /= 1 + x;
329        else arbM = i * (int)arbM + try, arbL = i;
330      }
331    }
332    L = preL * arbL, M = (int)(arbM * postM), x = (L|M)&1, L >>= !x, M >>= !x;
333    if (iOpt && postL == 1 && (d = preL * arbL / arbM) > 4 && d != 5) {
334      for (postL = 4, i = (int)(d / 16); (i >>= 1) && postL < 256; postL <<= 1);
335      arbM = arbM * postL / arbL / preL, arbL = 1, n = 0;
336    } else if (rational && (max(L, M) < 3 + 2 * iOpt || L * M < 6 * iOpt))
337      preL = L, preM = M, arbM = arbL = postM = 1;
338    if (!mode && (!rational || !n))
339      ++mode, n = 0;
340  }
341
342  p->num_stages = shr + have_pre_stage + have_arb_stage + have_post_stage;
343  if (!p->num_stages && multiplier != 1) {
344    bits = arbL = 0;                         /* Use cubic_stage in this case. */
345    ++p->num_stages;
346  }
347  p->stages = calloc((size_t)p->num_stages + 1, sizeof(*p->stages));
348  if (!p->stages)
349    return "out of memory";
350  for (i = 0; i < p->num_stages; ++i) {
351    p->stages[i].num = i;
352    p->stages[i].shared = shared;
353    p->stages[i].input_size = 8192;
354  }
355  p->stages[0].is_input = true;
356
357  alpha = postM / (io_ratio * (postL << 0));
358
359  if ((n = p->num_stages) > 1) {                              /* Att. budget: */
360    if (have_arb_stage)
361      att += linear_to_dB(2.), attArb = att, --n;
362    att += linear_to_dB((double)n);
363  }
364
365  half_fir_info = find_half_fir(core->half_firs, core->half_firs_len, att);
366  for (i = 0, s = p->stages; i < shr; ++i, ++s) {
367    s->fn = half_fir_info->fn;
368    s->coefs = half_fir_info->coefs;
369    s->n = half_fir_info->num_coefs;
370    s->pre_post = 4 * s->n;
371    s->preload = s->pre = s->pre_post >> 1;
372  }
373
374  if (have_pre_stage) {
375    if (maintain_3dB_pt && have_post_stage) {    /* Trans. bands overlapping. */
376      double x = tbw0 * lsx_inv_f_resp(-3., att);
377      x = -lsx_f_resp(x / (max(2 * alpha - Fs0, alpha) - Fp0), att);
378      if (x > .035) {
379        tbw_tighten = ((4.3074e-3 - 3.9121e-4 * x) * x - .040009) * x + 1.0014;
380        lsx_debug("tbw_tighten=%g (%gdB)", tbw_tighten, x);
381      }
382    }
383    Fn1 = preM? max(preL, preM) : arbM / arbL;
384    dft_stage_init(0, tighten(Fp1), Fs1, Fn1, att, phase_response, s++, preL,
385        max(preM, 1), &multiplier, r_spec->log2_min_dft_size,
386        r_spec->log2_large_dft_size, core_flags, core->rdft_cb);
387    Fp1 /= Fn1, Fs1 /= Fn1;
388  }
389
390  if (bits==0 && have_arb_stage) {                /* `Quick' cubic arb stage: */
391    s->fn = core->cubic_stage_fn;
392    s->mult = multiplier, multiplier = 1;
393    s->step.whole = (int64_t)(arbM * MULT32 + .5);
394    s->pre_post = max(3, s->step.integer);
395    s->preload = s->pre = 1;
396    s->out_in_ratio = MULT32 / (double)s->step.whole;
397  }
398  else if (have_arb_stage) {                     /* Higher quality arb stage: */
399    static const float rolloffs[] = {-.01f, -.3f, 0, -.103f};
400    poly_fir_t const * f = &core->poly_firs[6*(upsample+!!preM)+mode-!upsample];
401    int order, num_coefs = (int)f->interp[0].scalar, phase_bits, phases;
402    size_t coefs_size;
403    double at, Fp = Fp1, Fs, Fn, mult = upsample? 1 : arbM / arbL;
404    poly_fir1_t const * f1;
405
406    if (!upsample && preM)
407      Fn = 2 * mult, Fs = 3 + fabs(Fs1 - 1);
408    else Fn = 1, Fs = 2 - (mode? Fp1 + (Fs1 - Fp1) * .7 : Fs1);
409
410    if (mode)
411      Fp = Fs - (Fs - Fp) / (1 - lsx_inv_f_resp(rolloffs[rolloff], attArb));
412
413    i = (interpolator < 0? !rational : max(interpolator, !rational)) - 1;
414    do {
415      f1 = &f->interp[++i];
416      assert(f1->fn);
417      if (i)
418        arbM /= arbL, arbL = 1, rational = false;
419      phase_bits = (int)ceil(f1->scalar - log(mult)/log(2.));
420      phases = !rational? (1 << phase_bits) : arbL;
421      if (f->interp[0].scalar==0) {
422        int phases0 = max(phases, 19), n0 = 0;
423        lsx_design_lpf(Fp, Fs, -Fn, attArb, &n0, phases0, f->beta);
424        num_coefs = n0 / phases0 + 1, num_coefs += num_coefs & !preM;
425      }
426      if ((num_coefs & 1) && rational && (arbL & 1))
427        phases <<= 1, arbL <<= 1, arbM *= 2;
428      at = arbL * (s->phase0 = .5 * (num_coefs & 1));
429      order = i + (i && mode > 4);
430      coefs_size = (size_t)(num_coefs4 * phases * (order+1)) * sizeof_real;
431    } while (interpolator < 0 && i < 2 && f->interp[i+1].fn &&
432        coefs_size / 1000 > r_spec->coef_size_kbytes);
433
434    if (!s->shared->poly_fir_coefs) {
435      int num_taps = num_coefs * phases - 1;
436      double * coefs = lsx_design_lpf(
437          Fp, Fs, Fn, attArb, &num_taps, phases, f->beta);
438      s->shared->poly_fir_coefs = prepare_poly_fir_coefs(
439          coefs, num_coefs, phases, order, multiplier, core_flags, &core->mem);
440      lsx_debug("fir_len=%i phases=%i coef_interp=%i size=%.3gk",
441          num_coefs, phases, order, (double)coefs_size / 1000.);
442      free(coefs);
443    }
444    multiplier = 1;
445    s->fn = f1->fn;
446    s->pre_post = num_coefs4 - 1;
447    s->preload = ((num_coefs - 1) >> 1) + (num_coefs4 - num_coefs);
448    s->n = num_coefs4;
449    s->phase_bits = phase_bits;
450    s->L = arbL;
451    s->use_hi_prec_clock =
452      mode>1 && (q_spec->flags & SOXR_HI_PREC_CLOCK) && !rational;
453#if WITH_FLOAT_STD_PREC_CLOCK
454    if (order && !s->use_hi_prec_clock) {
455      s->at.flt = at;
456      s->step.flt = arbM;
457      s->out_in_ratio = (double)(arbL / s->step.flt);
458    } else
459#endif
460    {
461      s->at.whole = (int64_t)(at * MULT32 + .5);
462#if WITH_HI_PREC_CLOCK
463      if (s->use_hi_prec_clock) {
464        double M = arbM * MULT32;
465        s->at.fix.ls.parts.ms = 0x80000000ul;
466        s->step.whole = (int64_t)M;
467        M -= (double)s->step.whole;
468        M *= MULT32 * MULT32;
469        s->step.fix.ls.all = (uint64_t)M;
470      } else
471#endif
472        s->step.whole = (int64_t)(arbM * MULT32 + .5);
473      s->out_in_ratio = MULT32 * arbL / (double)s->step.whole;
474    }
475    ++s;
476  }
477
478  if (have_post_stage)
479    dft_stage_init(1, tighten(Fp0 / (upsample? alpha : 1)), upsample? max(2 -
480        Fs0 / alpha, 1) : Fs0, (double)max(postL, postM), att, phase_response,
481        s++, postL, postM, &multiplier, r_spec->log2_min_dft_size,
482        r_spec->log2_large_dft_size, core_flags, core->rdft_cb);
483
484  lsx_debug("%g: >>%i %i/%i %i/%g %i/%i (%x)", 1/io_ratio,
485      shr, preL, preM, arbL, arbM, postL, postM, core_flags);
486
487  for (i = 0, s = p->stages; i < p->num_stages; ++i, ++s) {
488    fifo_create(&s->fifo, (int)sizeof_real);
489    memset(fifo_reserve(&s->fifo, s->preload), 0,
490        sizeof_real * (size_t)s->preload);
491    lsx_debug_more("%5i|%-5i preload=%i remL=%i",
492        s->pre, s->pre_post-s->pre, s->preload, s->at.integer);
493  }
494  fifo_create(&s->fifo, (int)sizeof_real);
495  return 0;
496}
497
498static bool stage_process(stage_t * stage, bool flushing)
499{
500  fifo_t * fifo = &stage->fifo;
501  bool done = false;
502  int want;
503  while (!done && (want = stage->input_size - fifo_occupancy(fifo)) > 0) {
504    if (stage->is_input) {
505      if (flushing)
506        memset(fifo_reserve(fifo, want), 0, fifo->item_size * (size_t)want);
507      else done = true;
508    }
509    else done = stage_process(stage - 1, flushing);
510  }
511  stage->fn(stage, &stage[1].fifo);
512  return done && fifo_occupancy(fifo) < stage->input_size;
513}
514
515STATIC void _soxr_process(rate_t * p, size_t olen)
516{
517  int const n = p->flushing? min(-(int)p->samples_out, (int)olen) : (int)olen;
518  stage_t * stage = &p->stages[p->num_stages];
519  fifo_t * fifo = &stage->fifo;
520  bool done = false;
521  while (!done && fifo_occupancy(fifo) < (int)n)
522    done = stage->is_input || stage_process(stage - 1, p->flushing);
523}
524
525STATIC real * _soxr_input(rate_t * p, real const * samples, size_t n)
526{
527  if (p->flushing)
528    return 0;
529  p->samples_in += (int64_t)n;
530  return fifo_write(&p->stages[0].fifo, (int)n, samples);
531}
532
533STATIC real const * _soxr_output(rate_t * p, real * samples, size_t * n0)
534{
535  fifo_t * fifo = &p->stages[p->num_stages].fifo;
536  int n = p->flushing? min(-(int)p->samples_out, (int)*n0) : (int)*n0;
537  p->samples_out += n = min(n, fifo_occupancy(fifo));
538  return fifo_read(fifo, (int)(*n0 = (size_t)n), samples);
539}
540
541STATIC void _soxr_flush(rate_t * p)
542{
543  if (p->flushing) return;
544  p->samples_out -= (int64_t)((double)p->samples_in / p->io_ratio + .5);
545  p->samples_in = 0;
546  p->flushing = true;
547}
548
549STATIC void _soxr_close(rate_t * p)
550{
551  if (p->stages) {
552    fn_t const * const RDFT_CB = p->core->rdft_cb;
553    rate_shared_t * shared = p->stages[0].shared;
554    int i;
555
556    for (i = 0; i <= p->num_stages; ++i) {
557      stage_t * s = &p->stages[i];
558      rdft_free(s->dft_scratch);
559      rdft_free(s->dft_out);
560      fifo_delete(&s->fifo);
561    }
562    if (shared) {
563      for (i = 0; i < 2; ++i) {
564        dft_filter_t * f= &shared->dft_filter[i];
565        rdft_free(f->coefs);
566        rdft_delete_setup(f->dft_forward_setup);
567        rdft_delete_setup(f->dft_backward_setup);
568      }
569      p->core->mem.free(shared->poly_fir_coefs);
570      memset(shared, 0, sizeof(*shared));
571    }
572    free(p->stages);
573    (void)RDFT_CB;
574  }
575}
576
577#if defined SOXR_LIB
578STATIC double _soxr_delay(rate_t * p)
579{
580  return (double)p->samples_in / p->io_ratio - (double)p->samples_out;
581}
582
583STATIC void _soxr_sizes(size_t * shared, size_t * channel)
584{
585  *shared = sizeof(rate_shared_t);
586  *channel = sizeof(rate_t);
587}
588#endif