153a5a1b3Sopenharmony_ci/* Copyright (C) 2007-2008 Jean-Marc Valin
253a5a1b3Sopenharmony_ci   Copyright (C) 2008      Thorvald Natvig
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci   File: resample.c
553a5a1b3Sopenharmony_ci   Arbitrary resampling code
653a5a1b3Sopenharmony_ci
753a5a1b3Sopenharmony_ci   Redistribution and use in source and binary forms, with or without
853a5a1b3Sopenharmony_ci   modification, are permitted provided that the following conditions are
953a5a1b3Sopenharmony_ci   met:
1053a5a1b3Sopenharmony_ci
1153a5a1b3Sopenharmony_ci   1. Redistributions of source code must retain the above copyright notice,
1253a5a1b3Sopenharmony_ci   this list of conditions and the following disclaimer.
1353a5a1b3Sopenharmony_ci
1453a5a1b3Sopenharmony_ci   2. Redistributions in binary form must reproduce the above copyright
1553a5a1b3Sopenharmony_ci   notice, this list of conditions and the following disclaimer in the
1653a5a1b3Sopenharmony_ci   documentation and/or other materials provided with the distribution.
1753a5a1b3Sopenharmony_ci
1853a5a1b3Sopenharmony_ci   3. The name of the author may not be used to endorse or promote products
1953a5a1b3Sopenharmony_ci   derived from this software without specific prior written permission.
2053a5a1b3Sopenharmony_ci
2153a5a1b3Sopenharmony_ci   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2253a5a1b3Sopenharmony_ci   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2353a5a1b3Sopenharmony_ci   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2453a5a1b3Sopenharmony_ci   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2553a5a1b3Sopenharmony_ci   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2653a5a1b3Sopenharmony_ci   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2753a5a1b3Sopenharmony_ci   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2853a5a1b3Sopenharmony_ci   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2953a5a1b3Sopenharmony_ci   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3053a5a1b3Sopenharmony_ci   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3153a5a1b3Sopenharmony_ci   POSSIBILITY OF SUCH DAMAGE.
3253a5a1b3Sopenharmony_ci*/
3353a5a1b3Sopenharmony_ci
3453a5a1b3Sopenharmony_ci/*
3553a5a1b3Sopenharmony_ci   The design goals of this code are:
3653a5a1b3Sopenharmony_ci      - Very fast algorithm
3753a5a1b3Sopenharmony_ci      - SIMD-friendly algorithm
3853a5a1b3Sopenharmony_ci      - Low memory requirement
3953a5a1b3Sopenharmony_ci      - Good *perceptual* quality (and not best SNR)
4053a5a1b3Sopenharmony_ci
4153a5a1b3Sopenharmony_ci   Warning: This resampler is relatively new. Although I think I got rid of
4253a5a1b3Sopenharmony_ci   all the major bugs and I don't expect the API to change anymore, there
4353a5a1b3Sopenharmony_ci   may be something I've missed. So use with caution.
4453a5a1b3Sopenharmony_ci
4553a5a1b3Sopenharmony_ci   This algorithm is based on this original resampling algorithm:
4653a5a1b3Sopenharmony_ci   Smith, Julius O. Digital Audio Resampling Home Page
4753a5a1b3Sopenharmony_ci   Center for Computer Research in Music and Acoustics (CCRMA),
4853a5a1b3Sopenharmony_ci   Stanford University, 2007.
4953a5a1b3Sopenharmony_ci   Web published at https://ccrma.stanford.edu/~jos/resample/.
5053a5a1b3Sopenharmony_ci
5153a5a1b3Sopenharmony_ci   There is one main difference, though. This resampler uses cubic
5253a5a1b3Sopenharmony_ci   interpolation instead of linear interpolation in the above paper. This
5353a5a1b3Sopenharmony_ci   makes the table much smaller and makes it possible to compute that table
5453a5a1b3Sopenharmony_ci   on a per-stream basis. In turn, being able to tweak the table for each
5553a5a1b3Sopenharmony_ci   stream makes it possible to both reduce complexity on simple ratios
5653a5a1b3Sopenharmony_ci   (e.g. 2/3), and get rid of the rounding operations in the inner loop.
5753a5a1b3Sopenharmony_ci   The latter both reduces CPU time and makes the algorithm more SIMD-friendly.
5853a5a1b3Sopenharmony_ci*/
5953a5a1b3Sopenharmony_ci
6053a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H
6153a5a1b3Sopenharmony_ci#include "config.h"
6253a5a1b3Sopenharmony_ci#endif
6353a5a1b3Sopenharmony_ci
6453a5a1b3Sopenharmony_ci#ifdef OUTSIDE_SPEEX
6553a5a1b3Sopenharmony_ci#include <stdlib.h>
6653a5a1b3Sopenharmony_cistatic void *speex_alloc(int size) {return calloc(size,1);}
6753a5a1b3Sopenharmony_cistatic void *speex_realloc(void *ptr, int size) {return realloc(ptr, size);}
6853a5a1b3Sopenharmony_cistatic void speex_free(void *ptr) {free(ptr);}
6953a5a1b3Sopenharmony_ci#ifndef EXPORT
7053a5a1b3Sopenharmony_ci#define EXPORT
7153a5a1b3Sopenharmony_ci#endif
7253a5a1b3Sopenharmony_ci#include "speex_resampler.h"
7353a5a1b3Sopenharmony_ci#include "arch.h"
7453a5a1b3Sopenharmony_ci#else /* OUTSIDE_SPEEX */
7553a5a1b3Sopenharmony_ci
7653a5a1b3Sopenharmony_ci#include "speex/speex_resampler.h"
7753a5a1b3Sopenharmony_ci#include "arch.h"
7853a5a1b3Sopenharmony_ci#include "os_support.h"
7953a5a1b3Sopenharmony_ci#endif /* OUTSIDE_SPEEX */
8053a5a1b3Sopenharmony_ci
8153a5a1b3Sopenharmony_ci#include <math.h>
8253a5a1b3Sopenharmony_ci#include <limits.h>
8353a5a1b3Sopenharmony_ci
8453a5a1b3Sopenharmony_ci#ifndef M_PI
8553a5a1b3Sopenharmony_ci#define M_PI 3.14159265358979323846
8653a5a1b3Sopenharmony_ci#endif
8753a5a1b3Sopenharmony_ci
8853a5a1b3Sopenharmony_ci#define IMAX(a,b) ((a) > (b) ? (a) : (b))
8953a5a1b3Sopenharmony_ci#define IMIN(a,b) ((a) < (b) ? (a) : (b))
9053a5a1b3Sopenharmony_ci
9153a5a1b3Sopenharmony_ci#ifndef NULL
9253a5a1b3Sopenharmony_ci#define NULL 0
9353a5a1b3Sopenharmony_ci#endif
9453a5a1b3Sopenharmony_ci
9553a5a1b3Sopenharmony_ci#ifndef UINT32_MAX
9653a5a1b3Sopenharmony_ci#define UINT32_MAX 4294967295U
9753a5a1b3Sopenharmony_ci#endif
9853a5a1b3Sopenharmony_ci
9953a5a1b3Sopenharmony_ci#ifdef USE_SSE
10053a5a1b3Sopenharmony_ci#include "resample_sse.h"
10153a5a1b3Sopenharmony_ci#endif
10253a5a1b3Sopenharmony_ci
10353a5a1b3Sopenharmony_ci#ifdef USE_NEON
10453a5a1b3Sopenharmony_ci#include "resample_neon.h"
10553a5a1b3Sopenharmony_ci#endif
10653a5a1b3Sopenharmony_ci
10753a5a1b3Sopenharmony_ci/* Numer of elements to allocate on the stack */
10853a5a1b3Sopenharmony_ci#ifdef VAR_ARRAYS
10953a5a1b3Sopenharmony_ci#define FIXED_STACK_ALLOC 8192
11053a5a1b3Sopenharmony_ci#else
11153a5a1b3Sopenharmony_ci#define FIXED_STACK_ALLOC 1024
11253a5a1b3Sopenharmony_ci#endif
11353a5a1b3Sopenharmony_ci
11453a5a1b3Sopenharmony_ci#define EXPORT __attribute__((visibility("default")))
11553a5a1b3Sopenharmony_ci
11653a5a1b3Sopenharmony_citypedef int (*resampler_basic_func)(SpeexResamplerState *, spx_uint32_t , const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *);
11753a5a1b3Sopenharmony_ci
11853a5a1b3Sopenharmony_cistruct SpeexResamplerState_ {
11953a5a1b3Sopenharmony_ci   spx_uint32_t in_rate;
12053a5a1b3Sopenharmony_ci   spx_uint32_t out_rate;
12153a5a1b3Sopenharmony_ci   spx_uint32_t num_rate;
12253a5a1b3Sopenharmony_ci   spx_uint32_t den_rate;
12353a5a1b3Sopenharmony_ci
12453a5a1b3Sopenharmony_ci   int    quality;
12553a5a1b3Sopenharmony_ci   spx_uint32_t nb_channels;
12653a5a1b3Sopenharmony_ci   spx_uint32_t filt_len;
12753a5a1b3Sopenharmony_ci   spx_uint32_t mem_alloc_size;
12853a5a1b3Sopenharmony_ci   spx_uint32_t buffer_size;
12953a5a1b3Sopenharmony_ci   int          int_advance;
13053a5a1b3Sopenharmony_ci   int          frac_advance;
13153a5a1b3Sopenharmony_ci   float  cutoff;
13253a5a1b3Sopenharmony_ci   spx_uint32_t oversample;
13353a5a1b3Sopenharmony_ci   int          initialised;
13453a5a1b3Sopenharmony_ci   int          started;
13553a5a1b3Sopenharmony_ci
13653a5a1b3Sopenharmony_ci   /* These are per-channel */
13753a5a1b3Sopenharmony_ci   spx_int32_t  *last_sample;
13853a5a1b3Sopenharmony_ci   spx_uint32_t *samp_frac_num;
13953a5a1b3Sopenharmony_ci   spx_uint32_t *magic_samples;
14053a5a1b3Sopenharmony_ci
14153a5a1b3Sopenharmony_ci   spx_word16_t *mem;
14253a5a1b3Sopenharmony_ci   spx_word16_t *sinc_table;
14353a5a1b3Sopenharmony_ci   spx_uint32_t sinc_table_length;
14453a5a1b3Sopenharmony_ci   resampler_basic_func resampler_ptr;
14553a5a1b3Sopenharmony_ci
14653a5a1b3Sopenharmony_ci   int    in_stride;
14753a5a1b3Sopenharmony_ci   int    out_stride;
14853a5a1b3Sopenharmony_ci} ;
14953a5a1b3Sopenharmony_ci
15053a5a1b3Sopenharmony_cistatic const double kaiser12_table[68] = {
15153a5a1b3Sopenharmony_ci   0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076,
15253a5a1b3Sopenharmony_ci   0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014,
15353a5a1b3Sopenharmony_ci   0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601,
15453a5a1b3Sopenharmony_ci   0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014,
15553a5a1b3Sopenharmony_ci   0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490,
15653a5a1b3Sopenharmony_ci   0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546,
15753a5a1b3Sopenharmony_ci   0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178,
15853a5a1b3Sopenharmony_ci   0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947,
15953a5a1b3Sopenharmony_ci   0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058,
16053a5a1b3Sopenharmony_ci   0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438,
16153a5a1b3Sopenharmony_ci   0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734,
16253a5a1b3Sopenharmony_ci   0.00001000, 0.00000000};
16353a5a1b3Sopenharmony_ci/*
16453a5a1b3Sopenharmony_cistatic const double kaiser12_table[36] = {
16553a5a1b3Sopenharmony_ci   0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741,
16653a5a1b3Sopenharmony_ci   0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762,
16753a5a1b3Sopenharmony_ci   0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274,
16853a5a1b3Sopenharmony_ci   0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466,
16953a5a1b3Sopenharmony_ci   0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291,
17053a5a1b3Sopenharmony_ci   0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000};
17153a5a1b3Sopenharmony_ci*/
17253a5a1b3Sopenharmony_cistatic const double kaiser10_table[36] = {
17353a5a1b3Sopenharmony_ci   0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446,
17453a5a1b3Sopenharmony_ci   0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347,
17553a5a1b3Sopenharmony_ci   0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962,
17653a5a1b3Sopenharmony_ci   0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451,
17753a5a1b3Sopenharmony_ci   0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739,
17853a5a1b3Sopenharmony_ci   0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000};
17953a5a1b3Sopenharmony_ci
18053a5a1b3Sopenharmony_cistatic const double kaiser8_table[36] = {
18153a5a1b3Sopenharmony_ci   0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200,
18253a5a1b3Sopenharmony_ci   0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126,
18353a5a1b3Sopenharmony_ci   0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272,
18453a5a1b3Sopenharmony_ci   0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758,
18553a5a1b3Sopenharmony_ci   0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490,
18653a5a1b3Sopenharmony_ci   0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000};
18753a5a1b3Sopenharmony_ci
18853a5a1b3Sopenharmony_cistatic const double kaiser6_table[36] = {
18953a5a1b3Sopenharmony_ci   0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003,
19053a5a1b3Sopenharmony_ci   0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565,
19153a5a1b3Sopenharmony_ci   0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561,
19253a5a1b3Sopenharmony_ci   0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058,
19353a5a1b3Sopenharmony_ci   0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600,
19453a5a1b3Sopenharmony_ci   0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000};
19553a5a1b3Sopenharmony_ci
19653a5a1b3Sopenharmony_cistruct FuncDef {
19753a5a1b3Sopenharmony_ci   const double *table;
19853a5a1b3Sopenharmony_ci   int oversample;
19953a5a1b3Sopenharmony_ci};
20053a5a1b3Sopenharmony_ci
20153a5a1b3Sopenharmony_cistatic const struct FuncDef kaiser12_funcdef = {kaiser12_table, 64};
20253a5a1b3Sopenharmony_ci#define KAISER12 (&kaiser12_funcdef)
20353a5a1b3Sopenharmony_cistatic const struct FuncDef kaiser10_funcdef = {kaiser10_table, 32};
20453a5a1b3Sopenharmony_ci#define KAISER10 (&kaiser10_funcdef)
20553a5a1b3Sopenharmony_cistatic const struct FuncDef kaiser8_funcdef = {kaiser8_table, 32};
20653a5a1b3Sopenharmony_ci#define KAISER8 (&kaiser8_funcdef)
20753a5a1b3Sopenharmony_cistatic const struct FuncDef kaiser6_funcdef = {kaiser6_table, 32};
20853a5a1b3Sopenharmony_ci#define KAISER6 (&kaiser6_funcdef)
20953a5a1b3Sopenharmony_ci
21053a5a1b3Sopenharmony_cistruct QualityMapping {
21153a5a1b3Sopenharmony_ci   int base_length;
21253a5a1b3Sopenharmony_ci   int oversample;
21353a5a1b3Sopenharmony_ci   float downsample_bandwidth;
21453a5a1b3Sopenharmony_ci   float upsample_bandwidth;
21553a5a1b3Sopenharmony_ci   const struct FuncDef *window_func;
21653a5a1b3Sopenharmony_ci};
21753a5a1b3Sopenharmony_ci
21853a5a1b3Sopenharmony_ci
21953a5a1b3Sopenharmony_ci/* This table maps conversion quality to internal parameters. There are two
22053a5a1b3Sopenharmony_ci   reasons that explain why the up-sampling bandwidth is larger than the
22153a5a1b3Sopenharmony_ci   down-sampling bandwidth:
22253a5a1b3Sopenharmony_ci   1) When up-sampling, we can assume that the spectrum is already attenuated
22353a5a1b3Sopenharmony_ci      close to the Nyquist rate (from an A/D or a previous resampling filter)
22453a5a1b3Sopenharmony_ci   2) Any aliasing that occurs very close to the Nyquist rate will be masked
22553a5a1b3Sopenharmony_ci      by the sinusoids/noise just below the Nyquist rate (guaranteed only for
22653a5a1b3Sopenharmony_ci      up-sampling).
22753a5a1b3Sopenharmony_ci*/
22853a5a1b3Sopenharmony_cistatic const struct QualityMapping quality_map[11] = {
22953a5a1b3Sopenharmony_ci   {  8,  4, 0.830f, 0.860f, KAISER6 }, /* Q0 */
23053a5a1b3Sopenharmony_ci   { 16,  4, 0.850f, 0.880f, KAISER6 }, /* Q1 */
23153a5a1b3Sopenharmony_ci   { 32,  4, 0.882f, 0.910f, KAISER6 }, /* Q2 */  /* 82.3% cutoff ( ~60 dB stop) 6  */
23253a5a1b3Sopenharmony_ci   { 48,  8, 0.895f, 0.917f, KAISER8 }, /* Q3 */  /* 84.9% cutoff ( ~80 dB stop) 8  */
23353a5a1b3Sopenharmony_ci   { 64,  8, 0.921f, 0.940f, KAISER8 }, /* Q4 */  /* 88.7% cutoff ( ~80 dB stop) 8  */
23453a5a1b3Sopenharmony_ci   { 80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 */  /* 89.1% cutoff (~100 dB stop) 10 */
23553a5a1b3Sopenharmony_ci   { 96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 */  /* 91.5% cutoff (~100 dB stop) 10 */
23653a5a1b3Sopenharmony_ci   {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 */  /* 93.1% cutoff (~100 dB stop) 10 */
23753a5a1b3Sopenharmony_ci   {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 */  /* 94.5% cutoff (~100 dB stop) 10 */
23853a5a1b3Sopenharmony_ci   {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 */  /* 95.5% cutoff (~100 dB stop) 10 */
23953a5a1b3Sopenharmony_ci   {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 */ /* 96.6% cutoff (~100 dB stop) 10 */
24053a5a1b3Sopenharmony_ci};
24153a5a1b3Sopenharmony_ci/*8,24,40,56,80,104,128,160,200,256,320*/
24253a5a1b3Sopenharmony_cistatic double compute_func(float x, const struct FuncDef *func)
24353a5a1b3Sopenharmony_ci{
24453a5a1b3Sopenharmony_ci   float y, frac;
24553a5a1b3Sopenharmony_ci   double interp[4];
24653a5a1b3Sopenharmony_ci   int ind;
24753a5a1b3Sopenharmony_ci   y = x*func->oversample;
24853a5a1b3Sopenharmony_ci   ind = (int)floor(y);
24953a5a1b3Sopenharmony_ci   frac = (y-ind);
25053a5a1b3Sopenharmony_ci   /* CSE with handle the repeated powers */
25153a5a1b3Sopenharmony_ci   interp[3] =  -0.1666666667*frac + 0.1666666667*(frac*frac*frac);
25253a5a1b3Sopenharmony_ci   interp[2] = frac + 0.5*(frac*frac) - 0.5*(frac*frac*frac);
25353a5a1b3Sopenharmony_ci   /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
25453a5a1b3Sopenharmony_ci   interp[0] = -0.3333333333*frac + 0.5*(frac*frac) - 0.1666666667*(frac*frac*frac);
25553a5a1b3Sopenharmony_ci   /* Just to make sure we don't have rounding problems */
25653a5a1b3Sopenharmony_ci   interp[1] = 1.f-interp[3]-interp[2]-interp[0];
25753a5a1b3Sopenharmony_ci
25853a5a1b3Sopenharmony_ci   /*sum = frac*accum[1] + (1-frac)*accum[2];*/
25953a5a1b3Sopenharmony_ci   return interp[0]*func->table[ind] + interp[1]*func->table[ind+1] + interp[2]*func->table[ind+2] + interp[3]*func->table[ind+3];
26053a5a1b3Sopenharmony_ci}
26153a5a1b3Sopenharmony_ci
26253a5a1b3Sopenharmony_ci#if 0
26353a5a1b3Sopenharmony_ci#include <stdio.h>
26453a5a1b3Sopenharmony_ciint main(int argc, char **argv)
26553a5a1b3Sopenharmony_ci{
26653a5a1b3Sopenharmony_ci   int i;
26753a5a1b3Sopenharmony_ci   for (i=0;i<256;i++)
26853a5a1b3Sopenharmony_ci   {
26953a5a1b3Sopenharmony_ci      printf ("%f\n", compute_func(i/256., KAISER12));
27053a5a1b3Sopenharmony_ci   }
27153a5a1b3Sopenharmony_ci   return 0;
27253a5a1b3Sopenharmony_ci}
27353a5a1b3Sopenharmony_ci#endif
27453a5a1b3Sopenharmony_ci
27553a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
27653a5a1b3Sopenharmony_ci/* The slow way of computing a sinc for the table. Should improve that some day */
27753a5a1b3Sopenharmony_cistatic spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func)
27853a5a1b3Sopenharmony_ci{
27953a5a1b3Sopenharmony_ci   /*fprintf (stderr, "%f ", x);*/
28053a5a1b3Sopenharmony_ci   float xx = x * cutoff;
28153a5a1b3Sopenharmony_ci   if (fabs(x)<1e-6f)
28253a5a1b3Sopenharmony_ci      return WORD2INT(32768.*cutoff);
28353a5a1b3Sopenharmony_ci   else if (fabs(x) > .5f*N)
28453a5a1b3Sopenharmony_ci      return 0;
28553a5a1b3Sopenharmony_ci   /*FIXME: Can it really be any slower than this? */
28653a5a1b3Sopenharmony_ci   return WORD2INT(32768.*cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func));
28753a5a1b3Sopenharmony_ci}
28853a5a1b3Sopenharmony_ci#else
28953a5a1b3Sopenharmony_ci/* The slow way of computing a sinc for the table. Should improve that some day */
29053a5a1b3Sopenharmony_cistatic spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func)
29153a5a1b3Sopenharmony_ci{
29253a5a1b3Sopenharmony_ci   /*fprintf (stderr, "%f ", x);*/
29353a5a1b3Sopenharmony_ci   float xx = x * cutoff;
29453a5a1b3Sopenharmony_ci   if (fabs(x)<1e-6)
29553a5a1b3Sopenharmony_ci      return cutoff;
29653a5a1b3Sopenharmony_ci   else if (fabs(x) > .5*N)
29753a5a1b3Sopenharmony_ci      return 0;
29853a5a1b3Sopenharmony_ci   /*FIXME: Can it really be any slower than this? */
29953a5a1b3Sopenharmony_ci   return cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func);
30053a5a1b3Sopenharmony_ci}
30153a5a1b3Sopenharmony_ci#endif
30253a5a1b3Sopenharmony_ci
30353a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
30453a5a1b3Sopenharmony_cistatic void cubic_coef(spx_word16_t x, spx_word16_t interp[4])
30553a5a1b3Sopenharmony_ci{
30653a5a1b3Sopenharmony_ci   /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
30753a5a1b3Sopenharmony_ci   but I know it's MMSE-optimal on a sinc */
30853a5a1b3Sopenharmony_ci   spx_word16_t x2, x3;
30953a5a1b3Sopenharmony_ci   x2 = MULT16_16_P15(x, x);
31053a5a1b3Sopenharmony_ci   x3 = MULT16_16_P15(x, x2);
31153a5a1b3Sopenharmony_ci   interp[0] = PSHR32(MULT16_16(QCONST16(-0.16667f, 15),x) + MULT16_16(QCONST16(0.16667f, 15),x3),15);
31253a5a1b3Sopenharmony_ci   interp[1] = EXTRACT16(EXTEND32(x) + SHR32(SUB32(EXTEND32(x2),EXTEND32(x3)),1));
31353a5a1b3Sopenharmony_ci   interp[3] = PSHR32(MULT16_16(QCONST16(-0.33333f, 15),x) + MULT16_16(QCONST16(.5f,15),x2) - MULT16_16(QCONST16(0.16667f, 15),x3),15);
31453a5a1b3Sopenharmony_ci   /* Just to make sure we don't have rounding problems */
31553a5a1b3Sopenharmony_ci   interp[2] = Q15_ONE-interp[0]-interp[1]-interp[3];
31653a5a1b3Sopenharmony_ci   if (interp[2]<32767)
31753a5a1b3Sopenharmony_ci      interp[2]+=1;
31853a5a1b3Sopenharmony_ci}
31953a5a1b3Sopenharmony_ci#else
32053a5a1b3Sopenharmony_cistatic void cubic_coef(spx_word16_t frac, spx_word16_t interp[4])
32153a5a1b3Sopenharmony_ci{
32253a5a1b3Sopenharmony_ci   /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
32353a5a1b3Sopenharmony_ci   but I know it's MMSE-optimal on a sinc */
32453a5a1b3Sopenharmony_ci   interp[0] =  -0.16667f*frac + 0.16667f*frac*frac*frac;
32553a5a1b3Sopenharmony_ci   interp[1] = frac + 0.5f*frac*frac - 0.5f*frac*frac*frac;
32653a5a1b3Sopenharmony_ci   /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
32753a5a1b3Sopenharmony_ci   interp[3] = -0.33333f*frac + 0.5f*frac*frac - 0.16667f*frac*frac*frac;
32853a5a1b3Sopenharmony_ci   /* Just to make sure we don't have rounding problems */
32953a5a1b3Sopenharmony_ci   interp[2] = 1.-interp[0]-interp[1]-interp[3];
33053a5a1b3Sopenharmony_ci}
33153a5a1b3Sopenharmony_ci#endif
33253a5a1b3Sopenharmony_ci
33353a5a1b3Sopenharmony_cistatic int resampler_basic_direct_single(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
33453a5a1b3Sopenharmony_ci{
33553a5a1b3Sopenharmony_ci   const int N = st->filt_len;
33653a5a1b3Sopenharmony_ci   int out_sample = 0;
33753a5a1b3Sopenharmony_ci   int last_sample = st->last_sample[channel_index];
33853a5a1b3Sopenharmony_ci   spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
33953a5a1b3Sopenharmony_ci   const spx_word16_t *sinc_table = st->sinc_table;
34053a5a1b3Sopenharmony_ci   const int out_stride = st->out_stride;
34153a5a1b3Sopenharmony_ci   const int int_advance = st->int_advance;
34253a5a1b3Sopenharmony_ci   const int frac_advance = st->frac_advance;
34353a5a1b3Sopenharmony_ci   const spx_uint32_t den_rate = st->den_rate;
34453a5a1b3Sopenharmony_ci   spx_word32_t sum;
34553a5a1b3Sopenharmony_ci
34653a5a1b3Sopenharmony_ci   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
34753a5a1b3Sopenharmony_ci   {
34853a5a1b3Sopenharmony_ci      const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
34953a5a1b3Sopenharmony_ci      const spx_word16_t *iptr = & in[last_sample];
35053a5a1b3Sopenharmony_ci
35153a5a1b3Sopenharmony_ci#ifndef OVERRIDE_INNER_PRODUCT_SINGLE
35253a5a1b3Sopenharmony_ci      int j;
35353a5a1b3Sopenharmony_ci      sum = 0;
35453a5a1b3Sopenharmony_ci      for(j=0;j<N;j++) sum += MULT16_16(sinct[j], iptr[j]);
35553a5a1b3Sopenharmony_ci
35653a5a1b3Sopenharmony_ci/*    This code is slower on most DSPs which have only 2 accumulators.
35753a5a1b3Sopenharmony_ci      Plus this this forces truncation to 32 bits and you lose the HW guard bits.
35853a5a1b3Sopenharmony_ci      I think we can trust the compiler and let it vectorize and/or unroll itself.
35953a5a1b3Sopenharmony_ci      spx_word32_t accum[4] = {0,0,0,0};
36053a5a1b3Sopenharmony_ci      for(j=0;j<N;j+=4) {
36153a5a1b3Sopenharmony_ci        accum[0] += MULT16_16(sinct[j], iptr[j]);
36253a5a1b3Sopenharmony_ci        accum[1] += MULT16_16(sinct[j+1], iptr[j+1]);
36353a5a1b3Sopenharmony_ci        accum[2] += MULT16_16(sinct[j+2], iptr[j+2]);
36453a5a1b3Sopenharmony_ci        accum[3] += MULT16_16(sinct[j+3], iptr[j+3]);
36553a5a1b3Sopenharmony_ci      }
36653a5a1b3Sopenharmony_ci      sum = accum[0] + accum[1] + accum[2] + accum[3];
36753a5a1b3Sopenharmony_ci*/
36853a5a1b3Sopenharmony_ci      sum = SATURATE32PSHR(sum, 15, 32767);
36953a5a1b3Sopenharmony_ci#else
37053a5a1b3Sopenharmony_ci      sum = inner_product_single(sinct, iptr, N);
37153a5a1b3Sopenharmony_ci#endif
37253a5a1b3Sopenharmony_ci
37353a5a1b3Sopenharmony_ci      out[out_stride * out_sample++] = sum;
37453a5a1b3Sopenharmony_ci      last_sample += int_advance;
37553a5a1b3Sopenharmony_ci      samp_frac_num += frac_advance;
37653a5a1b3Sopenharmony_ci      if (samp_frac_num >= den_rate)
37753a5a1b3Sopenharmony_ci      {
37853a5a1b3Sopenharmony_ci         samp_frac_num -= den_rate;
37953a5a1b3Sopenharmony_ci         last_sample++;
38053a5a1b3Sopenharmony_ci      }
38153a5a1b3Sopenharmony_ci   }
38253a5a1b3Sopenharmony_ci
38353a5a1b3Sopenharmony_ci   st->last_sample[channel_index] = last_sample;
38453a5a1b3Sopenharmony_ci   st->samp_frac_num[channel_index] = samp_frac_num;
38553a5a1b3Sopenharmony_ci   return out_sample;
38653a5a1b3Sopenharmony_ci}
38753a5a1b3Sopenharmony_ci
38853a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
38953a5a1b3Sopenharmony_ci#else
39053a5a1b3Sopenharmony_ci/* This is the same as the previous function, except with a double-precision accumulator */
39153a5a1b3Sopenharmony_cistatic int resampler_basic_direct_double(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
39253a5a1b3Sopenharmony_ci{
39353a5a1b3Sopenharmony_ci   const int N = st->filt_len;
39453a5a1b3Sopenharmony_ci   int out_sample = 0;
39553a5a1b3Sopenharmony_ci   int last_sample = st->last_sample[channel_index];
39653a5a1b3Sopenharmony_ci   spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
39753a5a1b3Sopenharmony_ci   const spx_word16_t *sinc_table = st->sinc_table;
39853a5a1b3Sopenharmony_ci   const int out_stride = st->out_stride;
39953a5a1b3Sopenharmony_ci   const int int_advance = st->int_advance;
40053a5a1b3Sopenharmony_ci   const int frac_advance = st->frac_advance;
40153a5a1b3Sopenharmony_ci   const spx_uint32_t den_rate = st->den_rate;
40253a5a1b3Sopenharmony_ci   double sum;
40353a5a1b3Sopenharmony_ci
40453a5a1b3Sopenharmony_ci   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
40553a5a1b3Sopenharmony_ci   {
40653a5a1b3Sopenharmony_ci      const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
40753a5a1b3Sopenharmony_ci      const spx_word16_t *iptr = & in[last_sample];
40853a5a1b3Sopenharmony_ci
40953a5a1b3Sopenharmony_ci#ifndef OVERRIDE_INNER_PRODUCT_DOUBLE
41053a5a1b3Sopenharmony_ci      int j;
41153a5a1b3Sopenharmony_ci      double accum[4] = {0,0,0,0};
41253a5a1b3Sopenharmony_ci
41353a5a1b3Sopenharmony_ci      for(j=0;j<N;j+=4) {
41453a5a1b3Sopenharmony_ci        accum[0] += sinct[j]*iptr[j];
41553a5a1b3Sopenharmony_ci        accum[1] += sinct[j+1]*iptr[j+1];
41653a5a1b3Sopenharmony_ci        accum[2] += sinct[j+2]*iptr[j+2];
41753a5a1b3Sopenharmony_ci        accum[3] += sinct[j+3]*iptr[j+3];
41853a5a1b3Sopenharmony_ci      }
41953a5a1b3Sopenharmony_ci      sum = accum[0] + accum[1] + accum[2] + accum[3];
42053a5a1b3Sopenharmony_ci#else
42153a5a1b3Sopenharmony_ci      sum = inner_product_double(sinct, iptr, N);
42253a5a1b3Sopenharmony_ci#endif
42353a5a1b3Sopenharmony_ci
42453a5a1b3Sopenharmony_ci      out[out_stride * out_sample++] = PSHR32(sum, 15);
42553a5a1b3Sopenharmony_ci      last_sample += int_advance;
42653a5a1b3Sopenharmony_ci      samp_frac_num += frac_advance;
42753a5a1b3Sopenharmony_ci      if (samp_frac_num >= den_rate)
42853a5a1b3Sopenharmony_ci      {
42953a5a1b3Sopenharmony_ci         samp_frac_num -= den_rate;
43053a5a1b3Sopenharmony_ci         last_sample++;
43153a5a1b3Sopenharmony_ci      }
43253a5a1b3Sopenharmony_ci   }
43353a5a1b3Sopenharmony_ci
43453a5a1b3Sopenharmony_ci   st->last_sample[channel_index] = last_sample;
43553a5a1b3Sopenharmony_ci   st->samp_frac_num[channel_index] = samp_frac_num;
43653a5a1b3Sopenharmony_ci   return out_sample;
43753a5a1b3Sopenharmony_ci}
43853a5a1b3Sopenharmony_ci#endif
43953a5a1b3Sopenharmony_ci
44053a5a1b3Sopenharmony_cistatic int resampler_basic_interpolate_single(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
44153a5a1b3Sopenharmony_ci{
44253a5a1b3Sopenharmony_ci   const int N = st->filt_len;
44353a5a1b3Sopenharmony_ci   int out_sample = 0;
44453a5a1b3Sopenharmony_ci   int last_sample = st->last_sample[channel_index];
44553a5a1b3Sopenharmony_ci   spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
44653a5a1b3Sopenharmony_ci   const int out_stride = st->out_stride;
44753a5a1b3Sopenharmony_ci   const int int_advance = st->int_advance;
44853a5a1b3Sopenharmony_ci   const int frac_advance = st->frac_advance;
44953a5a1b3Sopenharmony_ci   const spx_uint32_t den_rate = st->den_rate;
45053a5a1b3Sopenharmony_ci   spx_word32_t sum;
45153a5a1b3Sopenharmony_ci
45253a5a1b3Sopenharmony_ci   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
45353a5a1b3Sopenharmony_ci   {
45453a5a1b3Sopenharmony_ci      const spx_word16_t *iptr = & in[last_sample];
45553a5a1b3Sopenharmony_ci
45653a5a1b3Sopenharmony_ci      const int offset = samp_frac_num*st->oversample/st->den_rate;
45753a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
45853a5a1b3Sopenharmony_ci      const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
45953a5a1b3Sopenharmony_ci#else
46053a5a1b3Sopenharmony_ci      const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
46153a5a1b3Sopenharmony_ci#endif
46253a5a1b3Sopenharmony_ci      spx_word16_t interp[4];
46353a5a1b3Sopenharmony_ci
46453a5a1b3Sopenharmony_ci
46553a5a1b3Sopenharmony_ci#ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
46653a5a1b3Sopenharmony_ci      int j;
46753a5a1b3Sopenharmony_ci      spx_word32_t accum[4] = {0,0,0,0};
46853a5a1b3Sopenharmony_ci
46953a5a1b3Sopenharmony_ci      for(j=0;j<N;j++) {
47053a5a1b3Sopenharmony_ci        const spx_word16_t curr_in=iptr[j];
47153a5a1b3Sopenharmony_ci        accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
47253a5a1b3Sopenharmony_ci        accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
47353a5a1b3Sopenharmony_ci        accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
47453a5a1b3Sopenharmony_ci        accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
47553a5a1b3Sopenharmony_ci      }
47653a5a1b3Sopenharmony_ci
47753a5a1b3Sopenharmony_ci      cubic_coef(frac, interp);
47853a5a1b3Sopenharmony_ci      sum = MULT16_32_Q15(interp[0],SHR32(accum[0], 1)) + MULT16_32_Q15(interp[1],SHR32(accum[1], 1)) + MULT16_32_Q15(interp[2],SHR32(accum[2], 1)) + MULT16_32_Q15(interp[3],SHR32(accum[3], 1));
47953a5a1b3Sopenharmony_ci      sum = SATURATE32PSHR(sum, 15, 32767);
48053a5a1b3Sopenharmony_ci#else
48153a5a1b3Sopenharmony_ci      cubic_coef(frac, interp);
48253a5a1b3Sopenharmony_ci      sum = interpolate_product_single(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
48353a5a1b3Sopenharmony_ci#endif
48453a5a1b3Sopenharmony_ci
48553a5a1b3Sopenharmony_ci      out[out_stride * out_sample++] = sum;
48653a5a1b3Sopenharmony_ci      last_sample += int_advance;
48753a5a1b3Sopenharmony_ci      samp_frac_num += frac_advance;
48853a5a1b3Sopenharmony_ci      if (samp_frac_num >= den_rate)
48953a5a1b3Sopenharmony_ci      {
49053a5a1b3Sopenharmony_ci         samp_frac_num -= den_rate;
49153a5a1b3Sopenharmony_ci         last_sample++;
49253a5a1b3Sopenharmony_ci      }
49353a5a1b3Sopenharmony_ci   }
49453a5a1b3Sopenharmony_ci
49553a5a1b3Sopenharmony_ci   st->last_sample[channel_index] = last_sample;
49653a5a1b3Sopenharmony_ci   st->samp_frac_num[channel_index] = samp_frac_num;
49753a5a1b3Sopenharmony_ci   return out_sample;
49853a5a1b3Sopenharmony_ci}
49953a5a1b3Sopenharmony_ci
50053a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
50153a5a1b3Sopenharmony_ci#else
50253a5a1b3Sopenharmony_ci/* This is the same as the previous function, except with a double-precision accumulator */
50353a5a1b3Sopenharmony_cistatic int resampler_basic_interpolate_double(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
50453a5a1b3Sopenharmony_ci{
50553a5a1b3Sopenharmony_ci   const int N = st->filt_len;
50653a5a1b3Sopenharmony_ci   int out_sample = 0;
50753a5a1b3Sopenharmony_ci   int last_sample = st->last_sample[channel_index];
50853a5a1b3Sopenharmony_ci   spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
50953a5a1b3Sopenharmony_ci   const int out_stride = st->out_stride;
51053a5a1b3Sopenharmony_ci   const int int_advance = st->int_advance;
51153a5a1b3Sopenharmony_ci   const int frac_advance = st->frac_advance;
51253a5a1b3Sopenharmony_ci   const spx_uint32_t den_rate = st->den_rate;
51353a5a1b3Sopenharmony_ci   spx_word32_t sum;
51453a5a1b3Sopenharmony_ci
51553a5a1b3Sopenharmony_ci   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
51653a5a1b3Sopenharmony_ci   {
51753a5a1b3Sopenharmony_ci      const spx_word16_t *iptr = & in[last_sample];
51853a5a1b3Sopenharmony_ci
51953a5a1b3Sopenharmony_ci      const int offset = samp_frac_num*st->oversample/st->den_rate;
52053a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
52153a5a1b3Sopenharmony_ci      const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
52253a5a1b3Sopenharmony_ci#else
52353a5a1b3Sopenharmony_ci      const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
52453a5a1b3Sopenharmony_ci#endif
52553a5a1b3Sopenharmony_ci      spx_word16_t interp[4];
52653a5a1b3Sopenharmony_ci
52753a5a1b3Sopenharmony_ci
52853a5a1b3Sopenharmony_ci#ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
52953a5a1b3Sopenharmony_ci      int j;
53053a5a1b3Sopenharmony_ci      double accum[4] = {0,0,0,0};
53153a5a1b3Sopenharmony_ci
53253a5a1b3Sopenharmony_ci      for(j=0;j<N;j++) {
53353a5a1b3Sopenharmony_ci        const double curr_in=iptr[j];
53453a5a1b3Sopenharmony_ci        accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
53553a5a1b3Sopenharmony_ci        accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
53653a5a1b3Sopenharmony_ci        accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
53753a5a1b3Sopenharmony_ci        accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
53853a5a1b3Sopenharmony_ci      }
53953a5a1b3Sopenharmony_ci
54053a5a1b3Sopenharmony_ci      cubic_coef(frac, interp);
54153a5a1b3Sopenharmony_ci      sum = MULT16_32_Q15(interp[0],accum[0]) + MULT16_32_Q15(interp[1],accum[1]) + MULT16_32_Q15(interp[2],accum[2]) + MULT16_32_Q15(interp[3],accum[3]);
54253a5a1b3Sopenharmony_ci#else
54353a5a1b3Sopenharmony_ci      cubic_coef(frac, interp);
54453a5a1b3Sopenharmony_ci      sum = interpolate_product_double(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
54553a5a1b3Sopenharmony_ci#endif
54653a5a1b3Sopenharmony_ci
54753a5a1b3Sopenharmony_ci      out[out_stride * out_sample++] = PSHR32(sum,15);
54853a5a1b3Sopenharmony_ci      last_sample += int_advance;
54953a5a1b3Sopenharmony_ci      samp_frac_num += frac_advance;
55053a5a1b3Sopenharmony_ci      if (samp_frac_num >= den_rate)
55153a5a1b3Sopenharmony_ci      {
55253a5a1b3Sopenharmony_ci         samp_frac_num -= den_rate;
55353a5a1b3Sopenharmony_ci         last_sample++;
55453a5a1b3Sopenharmony_ci      }
55553a5a1b3Sopenharmony_ci   }
55653a5a1b3Sopenharmony_ci
55753a5a1b3Sopenharmony_ci   st->last_sample[channel_index] = last_sample;
55853a5a1b3Sopenharmony_ci   st->samp_frac_num[channel_index] = samp_frac_num;
55953a5a1b3Sopenharmony_ci   return out_sample;
56053a5a1b3Sopenharmony_ci}
56153a5a1b3Sopenharmony_ci#endif
56253a5a1b3Sopenharmony_ci
56353a5a1b3Sopenharmony_ci/* This resampler is used to produce zero output in situations where memory
56453a5a1b3Sopenharmony_ci   for the filter could not be allocated.  The expected numbers of input and
56553a5a1b3Sopenharmony_ci   output samples are still processed so that callers failing to check error
56653a5a1b3Sopenharmony_ci   codes are not surprised, possibly getting into infinite loops. */
56753a5a1b3Sopenharmony_cistatic int resampler_basic_zero(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
56853a5a1b3Sopenharmony_ci{
56953a5a1b3Sopenharmony_ci   int out_sample = 0;
57053a5a1b3Sopenharmony_ci   int last_sample = st->last_sample[channel_index];
57153a5a1b3Sopenharmony_ci   spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
57253a5a1b3Sopenharmony_ci   const int out_stride = st->out_stride;
57353a5a1b3Sopenharmony_ci   const int int_advance = st->int_advance;
57453a5a1b3Sopenharmony_ci   const int frac_advance = st->frac_advance;
57553a5a1b3Sopenharmony_ci   const spx_uint32_t den_rate = st->den_rate;
57653a5a1b3Sopenharmony_ci
57753a5a1b3Sopenharmony_ci   (void)in;
57853a5a1b3Sopenharmony_ci   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
57953a5a1b3Sopenharmony_ci   {
58053a5a1b3Sopenharmony_ci      out[out_stride * out_sample++] = 0;
58153a5a1b3Sopenharmony_ci      last_sample += int_advance;
58253a5a1b3Sopenharmony_ci      samp_frac_num += frac_advance;
58353a5a1b3Sopenharmony_ci      if (samp_frac_num >= den_rate)
58453a5a1b3Sopenharmony_ci      {
58553a5a1b3Sopenharmony_ci         samp_frac_num -= den_rate;
58653a5a1b3Sopenharmony_ci         last_sample++;
58753a5a1b3Sopenharmony_ci      }
58853a5a1b3Sopenharmony_ci   }
58953a5a1b3Sopenharmony_ci
59053a5a1b3Sopenharmony_ci   st->last_sample[channel_index] = last_sample;
59153a5a1b3Sopenharmony_ci   st->samp_frac_num[channel_index] = samp_frac_num;
59253a5a1b3Sopenharmony_ci   return out_sample;
59353a5a1b3Sopenharmony_ci}
59453a5a1b3Sopenharmony_ci
59553a5a1b3Sopenharmony_cistatic int multiply_frac(spx_uint32_t *result, spx_uint32_t value, spx_uint32_t num, spx_uint32_t den)
59653a5a1b3Sopenharmony_ci{
59753a5a1b3Sopenharmony_ci   spx_uint32_t major = value / den;
59853a5a1b3Sopenharmony_ci   spx_uint32_t remain = value % den;
59953a5a1b3Sopenharmony_ci   /* TODO: Could use 64 bits operation to check for overflow. But only guaranteed in C99+ */
60053a5a1b3Sopenharmony_ci   if (remain > UINT32_MAX / num || major > UINT32_MAX / num
60153a5a1b3Sopenharmony_ci       || major * num > UINT32_MAX - remain * num / den)
60253a5a1b3Sopenharmony_ci      return RESAMPLER_ERR_OVERFLOW;
60353a5a1b3Sopenharmony_ci   *result = remain * num / den + major * num;
60453a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
60553a5a1b3Sopenharmony_ci}
60653a5a1b3Sopenharmony_ci
60753a5a1b3Sopenharmony_cistatic int update_filter(SpeexResamplerState *st)
60853a5a1b3Sopenharmony_ci{
60953a5a1b3Sopenharmony_ci   spx_uint32_t old_length = st->filt_len;
61053a5a1b3Sopenharmony_ci   spx_uint32_t old_alloc_size = st->mem_alloc_size;
61153a5a1b3Sopenharmony_ci   int use_direct;
61253a5a1b3Sopenharmony_ci   spx_uint32_t min_sinc_table_length;
61353a5a1b3Sopenharmony_ci   spx_uint32_t min_alloc_size;
61453a5a1b3Sopenharmony_ci
61553a5a1b3Sopenharmony_ci   st->int_advance = st->num_rate/st->den_rate;
61653a5a1b3Sopenharmony_ci   st->frac_advance = st->num_rate%st->den_rate;
61753a5a1b3Sopenharmony_ci   st->oversample = quality_map[st->quality].oversample;
61853a5a1b3Sopenharmony_ci   st->filt_len = quality_map[st->quality].base_length;
61953a5a1b3Sopenharmony_ci
62053a5a1b3Sopenharmony_ci   if (st->num_rate > st->den_rate)
62153a5a1b3Sopenharmony_ci   {
62253a5a1b3Sopenharmony_ci      /* down-sampling */
62353a5a1b3Sopenharmony_ci      st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate;
62453a5a1b3Sopenharmony_ci      if (multiply_frac(&st->filt_len,st->filt_len,st->num_rate,st->den_rate) != RESAMPLER_ERR_SUCCESS)
62553a5a1b3Sopenharmony_ci         goto fail;
62653a5a1b3Sopenharmony_ci      /* Round up to make sure we have a multiple of 8 for SSE */
62753a5a1b3Sopenharmony_ci      st->filt_len = ((st->filt_len-1)&(~0x7))+8;
62853a5a1b3Sopenharmony_ci      if (2*st->den_rate < st->num_rate)
62953a5a1b3Sopenharmony_ci         st->oversample >>= 1;
63053a5a1b3Sopenharmony_ci      if (4*st->den_rate < st->num_rate)
63153a5a1b3Sopenharmony_ci         st->oversample >>= 1;
63253a5a1b3Sopenharmony_ci      if (8*st->den_rate < st->num_rate)
63353a5a1b3Sopenharmony_ci         st->oversample >>= 1;
63453a5a1b3Sopenharmony_ci      if (16*st->den_rate < st->num_rate)
63553a5a1b3Sopenharmony_ci         st->oversample >>= 1;
63653a5a1b3Sopenharmony_ci      if (st->oversample < 1)
63753a5a1b3Sopenharmony_ci         st->oversample = 1;
63853a5a1b3Sopenharmony_ci   } else {
63953a5a1b3Sopenharmony_ci      /* up-sampling */
64053a5a1b3Sopenharmony_ci      st->cutoff = quality_map[st->quality].upsample_bandwidth;
64153a5a1b3Sopenharmony_ci   }
64253a5a1b3Sopenharmony_ci
64353a5a1b3Sopenharmony_ci#ifdef RESAMPLE_FULL_SINC_TABLE
64453a5a1b3Sopenharmony_ci   use_direct = 1;
64553a5a1b3Sopenharmony_ci   if (INT_MAX/sizeof(spx_word16_t)/st->den_rate < st->filt_len)
64653a5a1b3Sopenharmony_ci      goto fail;
64753a5a1b3Sopenharmony_ci#else
64853a5a1b3Sopenharmony_ci   /* Choose the resampling type that requires the least amount of memory */
64953a5a1b3Sopenharmony_ci   use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8
65053a5a1b3Sopenharmony_ci                && INT_MAX/sizeof(spx_word16_t)/st->den_rate >= st->filt_len;
65153a5a1b3Sopenharmony_ci#endif
65253a5a1b3Sopenharmony_ci   if (use_direct)
65353a5a1b3Sopenharmony_ci   {
65453a5a1b3Sopenharmony_ci      min_sinc_table_length = st->filt_len*st->den_rate;
65553a5a1b3Sopenharmony_ci   } else {
65653a5a1b3Sopenharmony_ci      if ((INT_MAX/sizeof(spx_word16_t)-8)/st->oversample < st->filt_len)
65753a5a1b3Sopenharmony_ci         goto fail;
65853a5a1b3Sopenharmony_ci
65953a5a1b3Sopenharmony_ci      min_sinc_table_length = st->filt_len*st->oversample+8;
66053a5a1b3Sopenharmony_ci   }
66153a5a1b3Sopenharmony_ci   if (st->sinc_table_length < min_sinc_table_length)
66253a5a1b3Sopenharmony_ci   {
66353a5a1b3Sopenharmony_ci      spx_word16_t *sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,min_sinc_table_length*sizeof(spx_word16_t));
66453a5a1b3Sopenharmony_ci      if (!sinc_table)
66553a5a1b3Sopenharmony_ci         goto fail;
66653a5a1b3Sopenharmony_ci
66753a5a1b3Sopenharmony_ci      st->sinc_table = sinc_table;
66853a5a1b3Sopenharmony_ci      st->sinc_table_length = min_sinc_table_length;
66953a5a1b3Sopenharmony_ci   }
67053a5a1b3Sopenharmony_ci   if (use_direct)
67153a5a1b3Sopenharmony_ci   {
67253a5a1b3Sopenharmony_ci      spx_uint32_t i;
67353a5a1b3Sopenharmony_ci      for (i=0;i<st->den_rate;i++)
67453a5a1b3Sopenharmony_ci      {
67553a5a1b3Sopenharmony_ci         spx_int32_t j;
67653a5a1b3Sopenharmony_ci         for (j=0;j<st->filt_len;j++)
67753a5a1b3Sopenharmony_ci         {
67853a5a1b3Sopenharmony_ci            st->sinc_table[i*st->filt_len+j] = sinc(st->cutoff,((j-(spx_int32_t)st->filt_len/2+1)-((float)i)/st->den_rate), st->filt_len, quality_map[st->quality].window_func);
67953a5a1b3Sopenharmony_ci         }
68053a5a1b3Sopenharmony_ci      }
68153a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
68253a5a1b3Sopenharmony_ci      st->resampler_ptr = resampler_basic_direct_single;
68353a5a1b3Sopenharmony_ci#else
68453a5a1b3Sopenharmony_ci      if (st->quality>8)
68553a5a1b3Sopenharmony_ci         st->resampler_ptr = resampler_basic_direct_double;
68653a5a1b3Sopenharmony_ci      else
68753a5a1b3Sopenharmony_ci         st->resampler_ptr = resampler_basic_direct_single;
68853a5a1b3Sopenharmony_ci#endif
68953a5a1b3Sopenharmony_ci      /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);*/
69053a5a1b3Sopenharmony_ci   } else {
69153a5a1b3Sopenharmony_ci      spx_int32_t i;
69253a5a1b3Sopenharmony_ci      for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++)
69353a5a1b3Sopenharmony_ci         st->sinc_table[i+4] = sinc(st->cutoff,(i/(float)st->oversample - st->filt_len/2), st->filt_len, quality_map[st->quality].window_func);
69453a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
69553a5a1b3Sopenharmony_ci      st->resampler_ptr = resampler_basic_interpolate_single;
69653a5a1b3Sopenharmony_ci#else
69753a5a1b3Sopenharmony_ci      if (st->quality>8)
69853a5a1b3Sopenharmony_ci         st->resampler_ptr = resampler_basic_interpolate_double;
69953a5a1b3Sopenharmony_ci      else
70053a5a1b3Sopenharmony_ci         st->resampler_ptr = resampler_basic_interpolate_single;
70153a5a1b3Sopenharmony_ci#endif
70253a5a1b3Sopenharmony_ci      /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/
70353a5a1b3Sopenharmony_ci   }
70453a5a1b3Sopenharmony_ci
70553a5a1b3Sopenharmony_ci   /* Here's the place where we update the filter memory to take into account
70653a5a1b3Sopenharmony_ci      the change in filter length. It's probably the messiest part of the code
70753a5a1b3Sopenharmony_ci      due to handling of lots of corner cases. */
70853a5a1b3Sopenharmony_ci
70953a5a1b3Sopenharmony_ci   /* Adding buffer_size to filt_len won't overflow here because filt_len
71053a5a1b3Sopenharmony_ci      could be multiplied by sizeof(spx_word16_t) above. */
71153a5a1b3Sopenharmony_ci   min_alloc_size = st->filt_len-1 + st->buffer_size;
71253a5a1b3Sopenharmony_ci   if (min_alloc_size > st->mem_alloc_size)
71353a5a1b3Sopenharmony_ci   {
71453a5a1b3Sopenharmony_ci      spx_word16_t *mem;
71553a5a1b3Sopenharmony_ci      if (INT_MAX/sizeof(spx_word16_t)/st->nb_channels < min_alloc_size)
71653a5a1b3Sopenharmony_ci          goto fail;
71753a5a1b3Sopenharmony_ci      else if (!(mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(*mem))))
71853a5a1b3Sopenharmony_ci          goto fail;
71953a5a1b3Sopenharmony_ci
72053a5a1b3Sopenharmony_ci      st->mem = mem;
72153a5a1b3Sopenharmony_ci      st->mem_alloc_size = min_alloc_size;
72253a5a1b3Sopenharmony_ci   }
72353a5a1b3Sopenharmony_ci   if (!st->started)
72453a5a1b3Sopenharmony_ci   {
72553a5a1b3Sopenharmony_ci      spx_uint32_t i;
72653a5a1b3Sopenharmony_ci      for (i=0;i<st->nb_channels*st->mem_alloc_size;i++)
72753a5a1b3Sopenharmony_ci         st->mem[i] = 0;
72853a5a1b3Sopenharmony_ci      /*speex_warning("reinit filter");*/
72953a5a1b3Sopenharmony_ci   } else if (st->filt_len > old_length)
73053a5a1b3Sopenharmony_ci   {
73153a5a1b3Sopenharmony_ci      spx_uint32_t i;
73253a5a1b3Sopenharmony_ci      /* Increase the filter length */
73353a5a1b3Sopenharmony_ci      /*speex_warning("increase filter size");*/
73453a5a1b3Sopenharmony_ci      for (i=st->nb_channels;i--;)
73553a5a1b3Sopenharmony_ci      {
73653a5a1b3Sopenharmony_ci         spx_uint32_t j;
73753a5a1b3Sopenharmony_ci         spx_uint32_t olen = old_length;
73853a5a1b3Sopenharmony_ci         /*if (st->magic_samples[i])*/
73953a5a1b3Sopenharmony_ci         {
74053a5a1b3Sopenharmony_ci            /* Try and remove the magic samples as if nothing had happened */
74153a5a1b3Sopenharmony_ci
74253a5a1b3Sopenharmony_ci            /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */
74353a5a1b3Sopenharmony_ci            olen = old_length + 2*st->magic_samples[i];
74453a5a1b3Sopenharmony_ci            for (j=old_length-1+st->magic_samples[i];j--;)
74553a5a1b3Sopenharmony_ci               st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]] = st->mem[i*old_alloc_size+j];
74653a5a1b3Sopenharmony_ci            for (j=0;j<st->magic_samples[i];j++)
74753a5a1b3Sopenharmony_ci               st->mem[i*st->mem_alloc_size+j] = 0;
74853a5a1b3Sopenharmony_ci            st->magic_samples[i] = 0;
74953a5a1b3Sopenharmony_ci         }
75053a5a1b3Sopenharmony_ci         if (st->filt_len > olen)
75153a5a1b3Sopenharmony_ci         {
75253a5a1b3Sopenharmony_ci            /* If the new filter length is still bigger than the "augmented" length */
75353a5a1b3Sopenharmony_ci            /* Copy data going backward */
75453a5a1b3Sopenharmony_ci            for (j=0;j<olen-1;j++)
75553a5a1b3Sopenharmony_ci               st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = st->mem[i*st->mem_alloc_size+(olen-2-j)];
75653a5a1b3Sopenharmony_ci            /* Then put zeros for lack of anything better */
75753a5a1b3Sopenharmony_ci            for (;j<st->filt_len-1;j++)
75853a5a1b3Sopenharmony_ci               st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = 0;
75953a5a1b3Sopenharmony_ci            /* Adjust last_sample */
76053a5a1b3Sopenharmony_ci            st->last_sample[i] += (st->filt_len - olen)/2;
76153a5a1b3Sopenharmony_ci         } else {
76253a5a1b3Sopenharmony_ci            /* Put back some of the magic! */
76353a5a1b3Sopenharmony_ci            st->magic_samples[i] = (olen - st->filt_len)/2;
76453a5a1b3Sopenharmony_ci            for (j=0;j<st->filt_len-1+st->magic_samples[i];j++)
76553a5a1b3Sopenharmony_ci               st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
76653a5a1b3Sopenharmony_ci         }
76753a5a1b3Sopenharmony_ci      }
76853a5a1b3Sopenharmony_ci   } else if (st->filt_len < old_length)
76953a5a1b3Sopenharmony_ci   {
77053a5a1b3Sopenharmony_ci      spx_uint32_t i;
77153a5a1b3Sopenharmony_ci      /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic"
77253a5a1b3Sopenharmony_ci         samples so they can be used directly as input the next time(s) */
77353a5a1b3Sopenharmony_ci      for (i=0;i<st->nb_channels;i++)
77453a5a1b3Sopenharmony_ci      {
77553a5a1b3Sopenharmony_ci         spx_uint32_t j;
77653a5a1b3Sopenharmony_ci         spx_uint32_t old_magic = st->magic_samples[i];
77753a5a1b3Sopenharmony_ci         st->magic_samples[i] = (old_length - st->filt_len)/2;
77853a5a1b3Sopenharmony_ci         /* We must copy some of the memory that's no longer used */
77953a5a1b3Sopenharmony_ci         /* Copy data going backward */
78053a5a1b3Sopenharmony_ci         for (j=0;j<st->filt_len-1+st->magic_samples[i]+old_magic;j++)
78153a5a1b3Sopenharmony_ci            st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
78253a5a1b3Sopenharmony_ci         st->magic_samples[i] += old_magic;
78353a5a1b3Sopenharmony_ci      }
78453a5a1b3Sopenharmony_ci   }
78553a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
78653a5a1b3Sopenharmony_ci
78753a5a1b3Sopenharmony_cifail:
78853a5a1b3Sopenharmony_ci   st->resampler_ptr = resampler_basic_zero;
78953a5a1b3Sopenharmony_ci   /* st->mem may still contain consumed input samples for the filter.
79053a5a1b3Sopenharmony_ci      Restore filt_len so that filt_len - 1 still points to the position after
79153a5a1b3Sopenharmony_ci      the last of these samples. */
79253a5a1b3Sopenharmony_ci   st->filt_len = old_length;
79353a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_ALLOC_FAILED;
79453a5a1b3Sopenharmony_ci}
79553a5a1b3Sopenharmony_ci
79653a5a1b3Sopenharmony_ciEXPORT SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
79753a5a1b3Sopenharmony_ci{
79853a5a1b3Sopenharmony_ci   return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err);
79953a5a1b3Sopenharmony_ci}
80053a5a1b3Sopenharmony_ci
80153a5a1b3Sopenharmony_ciEXPORT SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
80253a5a1b3Sopenharmony_ci{
80353a5a1b3Sopenharmony_ci   SpeexResamplerState *st;
80453a5a1b3Sopenharmony_ci   int filter_err;
80553a5a1b3Sopenharmony_ci
80653a5a1b3Sopenharmony_ci   if (nb_channels == 0 || ratio_num == 0 || ratio_den == 0 || quality > 10 || quality < 0)
80753a5a1b3Sopenharmony_ci   {
80853a5a1b3Sopenharmony_ci      if (err)
80953a5a1b3Sopenharmony_ci         *err = RESAMPLER_ERR_INVALID_ARG;
81053a5a1b3Sopenharmony_ci      return NULL;
81153a5a1b3Sopenharmony_ci   }
81253a5a1b3Sopenharmony_ci   st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
81353a5a1b3Sopenharmony_ci   if (!st)
81453a5a1b3Sopenharmony_ci   {
81553a5a1b3Sopenharmony_ci      if (err)
81653a5a1b3Sopenharmony_ci         *err = RESAMPLER_ERR_ALLOC_FAILED;
81753a5a1b3Sopenharmony_ci      return NULL;
81853a5a1b3Sopenharmony_ci   }
81953a5a1b3Sopenharmony_ci   st->initialised = 0;
82053a5a1b3Sopenharmony_ci   st->started = 0;
82153a5a1b3Sopenharmony_ci   st->in_rate = 0;
82253a5a1b3Sopenharmony_ci   st->out_rate = 0;
82353a5a1b3Sopenharmony_ci   st->num_rate = 0;
82453a5a1b3Sopenharmony_ci   st->den_rate = 0;
82553a5a1b3Sopenharmony_ci   st->quality = -1;
82653a5a1b3Sopenharmony_ci   st->sinc_table_length = 0;
82753a5a1b3Sopenharmony_ci   st->mem_alloc_size = 0;
82853a5a1b3Sopenharmony_ci   st->filt_len = 0;
82953a5a1b3Sopenharmony_ci   st->mem = 0;
83053a5a1b3Sopenharmony_ci   st->resampler_ptr = 0;
83153a5a1b3Sopenharmony_ci
83253a5a1b3Sopenharmony_ci   st->cutoff = 1.f;
83353a5a1b3Sopenharmony_ci   st->nb_channels = nb_channels;
83453a5a1b3Sopenharmony_ci   st->in_stride = 1;
83553a5a1b3Sopenharmony_ci   st->out_stride = 1;
83653a5a1b3Sopenharmony_ci
83753a5a1b3Sopenharmony_ci   st->buffer_size = 160;
83853a5a1b3Sopenharmony_ci
83953a5a1b3Sopenharmony_ci   /* Per channel data */
84053a5a1b3Sopenharmony_ci   if (!(st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t))))
84153a5a1b3Sopenharmony_ci      goto fail;
84253a5a1b3Sopenharmony_ci   if (!(st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
84353a5a1b3Sopenharmony_ci      goto fail;
84453a5a1b3Sopenharmony_ci   if (!(st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t))))
84553a5a1b3Sopenharmony_ci      goto fail;
84653a5a1b3Sopenharmony_ci
84753a5a1b3Sopenharmony_ci   speex_resampler_set_quality(st, quality);
84853a5a1b3Sopenharmony_ci   speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate);
84953a5a1b3Sopenharmony_ci
85053a5a1b3Sopenharmony_ci   filter_err = update_filter(st);
85153a5a1b3Sopenharmony_ci   if (filter_err == RESAMPLER_ERR_SUCCESS)
85253a5a1b3Sopenharmony_ci   {
85353a5a1b3Sopenharmony_ci      st->initialised = 1;
85453a5a1b3Sopenharmony_ci   } else {
85553a5a1b3Sopenharmony_ci      speex_resampler_destroy(st);
85653a5a1b3Sopenharmony_ci      st = NULL;
85753a5a1b3Sopenharmony_ci   }
85853a5a1b3Sopenharmony_ci   if (err)
85953a5a1b3Sopenharmony_ci      *err = filter_err;
86053a5a1b3Sopenharmony_ci
86153a5a1b3Sopenharmony_ci   return st;
86253a5a1b3Sopenharmony_ci
86353a5a1b3Sopenharmony_cifail:
86453a5a1b3Sopenharmony_ci   if (err)
86553a5a1b3Sopenharmony_ci      *err = RESAMPLER_ERR_ALLOC_FAILED;
86653a5a1b3Sopenharmony_ci   speex_resampler_destroy(st);
86753a5a1b3Sopenharmony_ci   return NULL;
86853a5a1b3Sopenharmony_ci}
86953a5a1b3Sopenharmony_ci
87053a5a1b3Sopenharmony_ciEXPORT void speex_resampler_destroy(SpeexResamplerState *st)
87153a5a1b3Sopenharmony_ci{
87253a5a1b3Sopenharmony_ci   speex_free(st->mem);
87353a5a1b3Sopenharmony_ci   speex_free(st->sinc_table);
87453a5a1b3Sopenharmony_ci   speex_free(st->last_sample);
87553a5a1b3Sopenharmony_ci   speex_free(st->magic_samples);
87653a5a1b3Sopenharmony_ci   speex_free(st->samp_frac_num);
87753a5a1b3Sopenharmony_ci   speex_free(st);
87853a5a1b3Sopenharmony_ci}
87953a5a1b3Sopenharmony_ci
88053a5a1b3Sopenharmony_cistatic int speex_resampler_process_native(SpeexResamplerState *st, spx_uint32_t channel_index, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
88153a5a1b3Sopenharmony_ci{
88253a5a1b3Sopenharmony_ci   int j=0;
88353a5a1b3Sopenharmony_ci   const int N = st->filt_len;
88453a5a1b3Sopenharmony_ci   int out_sample = 0;
88553a5a1b3Sopenharmony_ci   spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
88653a5a1b3Sopenharmony_ci   spx_uint32_t ilen;
88753a5a1b3Sopenharmony_ci
88853a5a1b3Sopenharmony_ci   st->started = 1;
88953a5a1b3Sopenharmony_ci
89053a5a1b3Sopenharmony_ci   /* Call the right resampler through the function ptr */
89153a5a1b3Sopenharmony_ci   out_sample = st->resampler_ptr(st, channel_index, mem, in_len, out, out_len);
89253a5a1b3Sopenharmony_ci
89353a5a1b3Sopenharmony_ci   if (st->last_sample[channel_index] < (spx_int32_t)*in_len)
89453a5a1b3Sopenharmony_ci      *in_len = st->last_sample[channel_index];
89553a5a1b3Sopenharmony_ci   *out_len = out_sample;
89653a5a1b3Sopenharmony_ci   st->last_sample[channel_index] -= *in_len;
89753a5a1b3Sopenharmony_ci
89853a5a1b3Sopenharmony_ci   ilen = *in_len;
89953a5a1b3Sopenharmony_ci
90053a5a1b3Sopenharmony_ci   for(j=0;j<N-1;++j)
90153a5a1b3Sopenharmony_ci     mem[j] = mem[j+ilen];
90253a5a1b3Sopenharmony_ci
90353a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
90453a5a1b3Sopenharmony_ci}
90553a5a1b3Sopenharmony_ci
90653a5a1b3Sopenharmony_cistatic int speex_resampler_magic(SpeexResamplerState *st, spx_uint32_t channel_index, spx_word16_t **out, spx_uint32_t out_len) {
90753a5a1b3Sopenharmony_ci   spx_uint32_t tmp_in_len = st->magic_samples[channel_index];
90853a5a1b3Sopenharmony_ci   spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
90953a5a1b3Sopenharmony_ci   const int N = st->filt_len;
91053a5a1b3Sopenharmony_ci
91153a5a1b3Sopenharmony_ci   speex_resampler_process_native(st, channel_index, &tmp_in_len, *out, &out_len);
91253a5a1b3Sopenharmony_ci
91353a5a1b3Sopenharmony_ci   st->magic_samples[channel_index] -= tmp_in_len;
91453a5a1b3Sopenharmony_ci
91553a5a1b3Sopenharmony_ci   /* If we couldn't process all "magic" input samples, save the rest for next time */
91653a5a1b3Sopenharmony_ci   if (st->magic_samples[channel_index])
91753a5a1b3Sopenharmony_ci   {
91853a5a1b3Sopenharmony_ci      spx_uint32_t i;
91953a5a1b3Sopenharmony_ci      for (i=0;i<st->magic_samples[channel_index];i++)
92053a5a1b3Sopenharmony_ci         mem[N-1+i]=mem[N-1+i+tmp_in_len];
92153a5a1b3Sopenharmony_ci   }
92253a5a1b3Sopenharmony_ci   *out += out_len*st->out_stride;
92353a5a1b3Sopenharmony_ci   return out_len;
92453a5a1b3Sopenharmony_ci}
92553a5a1b3Sopenharmony_ci
92653a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
92753a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
92853a5a1b3Sopenharmony_ci#else
92953a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
93053a5a1b3Sopenharmony_ci#endif
93153a5a1b3Sopenharmony_ci{
93253a5a1b3Sopenharmony_ci   int j;
93353a5a1b3Sopenharmony_ci   spx_uint32_t ilen = *in_len;
93453a5a1b3Sopenharmony_ci   spx_uint32_t olen = *out_len;
93553a5a1b3Sopenharmony_ci   spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
93653a5a1b3Sopenharmony_ci   const int filt_offs = st->filt_len - 1;
93753a5a1b3Sopenharmony_ci   const spx_uint32_t xlen = st->mem_alloc_size - filt_offs;
93853a5a1b3Sopenharmony_ci   const int istride = st->in_stride;
93953a5a1b3Sopenharmony_ci
94053a5a1b3Sopenharmony_ci   if (st->magic_samples[channel_index])
94153a5a1b3Sopenharmony_ci      olen -= speex_resampler_magic(st, channel_index, &out, olen);
94253a5a1b3Sopenharmony_ci   if (! st->magic_samples[channel_index]) {
94353a5a1b3Sopenharmony_ci      while (ilen && olen) {
94453a5a1b3Sopenharmony_ci        spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
94553a5a1b3Sopenharmony_ci        spx_uint32_t ochunk = olen;
94653a5a1b3Sopenharmony_ci
94753a5a1b3Sopenharmony_ci        if (in) {
94853a5a1b3Sopenharmony_ci           for(j=0;j<ichunk;++j)
94953a5a1b3Sopenharmony_ci              x[j+filt_offs]=in[j*istride];
95053a5a1b3Sopenharmony_ci        } else {
95153a5a1b3Sopenharmony_ci          for(j=0;j<ichunk;++j)
95253a5a1b3Sopenharmony_ci            x[j+filt_offs]=0;
95353a5a1b3Sopenharmony_ci        }
95453a5a1b3Sopenharmony_ci        speex_resampler_process_native(st, channel_index, &ichunk, out, &ochunk);
95553a5a1b3Sopenharmony_ci        ilen -= ichunk;
95653a5a1b3Sopenharmony_ci        olen -= ochunk;
95753a5a1b3Sopenharmony_ci        out += ochunk * st->out_stride;
95853a5a1b3Sopenharmony_ci        if (in)
95953a5a1b3Sopenharmony_ci           in += ichunk * istride;
96053a5a1b3Sopenharmony_ci      }
96153a5a1b3Sopenharmony_ci   }
96253a5a1b3Sopenharmony_ci   *in_len -= ilen;
96353a5a1b3Sopenharmony_ci   *out_len -= olen;
96453a5a1b3Sopenharmony_ci   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
96553a5a1b3Sopenharmony_ci}
96653a5a1b3Sopenharmony_ci
96753a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
96853a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
96953a5a1b3Sopenharmony_ci#else
97053a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
97153a5a1b3Sopenharmony_ci#endif
97253a5a1b3Sopenharmony_ci{
97353a5a1b3Sopenharmony_ci   int j;
97453a5a1b3Sopenharmony_ci   const int istride_save = st->in_stride;
97553a5a1b3Sopenharmony_ci   const int ostride_save = st->out_stride;
97653a5a1b3Sopenharmony_ci   spx_uint32_t ilen = *in_len;
97753a5a1b3Sopenharmony_ci   spx_uint32_t olen = *out_len;
97853a5a1b3Sopenharmony_ci   spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
97953a5a1b3Sopenharmony_ci   const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1);
98053a5a1b3Sopenharmony_ci#ifdef VAR_ARRAYS
98153a5a1b3Sopenharmony_ci   const unsigned int ylen = (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC;
98253a5a1b3Sopenharmony_ci   spx_word16_t ystack[ylen];
98353a5a1b3Sopenharmony_ci#else
98453a5a1b3Sopenharmony_ci   const unsigned int ylen = FIXED_STACK_ALLOC;
98553a5a1b3Sopenharmony_ci   spx_word16_t ystack[FIXED_STACK_ALLOC];
98653a5a1b3Sopenharmony_ci#endif
98753a5a1b3Sopenharmony_ci
98853a5a1b3Sopenharmony_ci   st->out_stride = 1;
98953a5a1b3Sopenharmony_ci
99053a5a1b3Sopenharmony_ci   while (ilen && olen) {
99153a5a1b3Sopenharmony_ci     spx_word16_t *y = ystack;
99253a5a1b3Sopenharmony_ci     spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
99353a5a1b3Sopenharmony_ci     spx_uint32_t ochunk = (olen > ylen) ? ylen : olen;
99453a5a1b3Sopenharmony_ci     spx_uint32_t omagic = 0;
99553a5a1b3Sopenharmony_ci
99653a5a1b3Sopenharmony_ci     if (st->magic_samples[channel_index]) {
99753a5a1b3Sopenharmony_ci       omagic = speex_resampler_magic(st, channel_index, &y, ochunk);
99853a5a1b3Sopenharmony_ci       ochunk -= omagic;
99953a5a1b3Sopenharmony_ci       olen -= omagic;
100053a5a1b3Sopenharmony_ci     }
100153a5a1b3Sopenharmony_ci     if (! st->magic_samples[channel_index]) {
100253a5a1b3Sopenharmony_ci       if (in) {
100353a5a1b3Sopenharmony_ci         for(j=0;j<ichunk;++j)
100453a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
100553a5a1b3Sopenharmony_ci           x[j+st->filt_len-1]=WORD2INT(in[j*istride_save]);
100653a5a1b3Sopenharmony_ci#else
100753a5a1b3Sopenharmony_ci           x[j+st->filt_len-1]=in[j*istride_save];
100853a5a1b3Sopenharmony_ci#endif
100953a5a1b3Sopenharmony_ci       } else {
101053a5a1b3Sopenharmony_ci         for(j=0;j<ichunk;++j)
101153a5a1b3Sopenharmony_ci           x[j+st->filt_len-1]=0;
101253a5a1b3Sopenharmony_ci       }
101353a5a1b3Sopenharmony_ci
101453a5a1b3Sopenharmony_ci       speex_resampler_process_native(st, channel_index, &ichunk, y, &ochunk);
101553a5a1b3Sopenharmony_ci     } else {
101653a5a1b3Sopenharmony_ci       ichunk = 0;
101753a5a1b3Sopenharmony_ci       ochunk = 0;
101853a5a1b3Sopenharmony_ci     }
101953a5a1b3Sopenharmony_ci
102053a5a1b3Sopenharmony_ci     for (j=0;j<ochunk+omagic;++j)
102153a5a1b3Sopenharmony_ci#ifdef FIXED_POINT
102253a5a1b3Sopenharmony_ci        out[j*ostride_save] = ystack[j];
102353a5a1b3Sopenharmony_ci#else
102453a5a1b3Sopenharmony_ci        out[j*ostride_save] = WORD2INT(ystack[j]);
102553a5a1b3Sopenharmony_ci#endif
102653a5a1b3Sopenharmony_ci
102753a5a1b3Sopenharmony_ci     ilen -= ichunk;
102853a5a1b3Sopenharmony_ci     olen -= ochunk;
102953a5a1b3Sopenharmony_ci     out += (ochunk+omagic) * ostride_save;
103053a5a1b3Sopenharmony_ci     if (in)
103153a5a1b3Sopenharmony_ci       in += ichunk * istride_save;
103253a5a1b3Sopenharmony_ci   }
103353a5a1b3Sopenharmony_ci   st->out_stride = ostride_save;
103453a5a1b3Sopenharmony_ci   *in_len -= ilen;
103553a5a1b3Sopenharmony_ci   *out_len -= olen;
103653a5a1b3Sopenharmony_ci
103753a5a1b3Sopenharmony_ci   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
103853a5a1b3Sopenharmony_ci}
103953a5a1b3Sopenharmony_ci
104053a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
104153a5a1b3Sopenharmony_ci{
104253a5a1b3Sopenharmony_ci   spx_uint32_t i;
104353a5a1b3Sopenharmony_ci   int istride_save, ostride_save;
104453a5a1b3Sopenharmony_ci   spx_uint32_t bak_out_len = *out_len;
104553a5a1b3Sopenharmony_ci   spx_uint32_t bak_in_len = *in_len;
104653a5a1b3Sopenharmony_ci   istride_save = st->in_stride;
104753a5a1b3Sopenharmony_ci   ostride_save = st->out_stride;
104853a5a1b3Sopenharmony_ci   st->in_stride = st->out_stride = st->nb_channels;
104953a5a1b3Sopenharmony_ci   for (i=0;i<st->nb_channels;i++)
105053a5a1b3Sopenharmony_ci   {
105153a5a1b3Sopenharmony_ci      *out_len = bak_out_len;
105253a5a1b3Sopenharmony_ci      *in_len = bak_in_len;
105353a5a1b3Sopenharmony_ci      if (in != NULL)
105453a5a1b3Sopenharmony_ci         speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len);
105553a5a1b3Sopenharmony_ci      else
105653a5a1b3Sopenharmony_ci         speex_resampler_process_float(st, i, NULL, in_len, out+i, out_len);
105753a5a1b3Sopenharmony_ci   }
105853a5a1b3Sopenharmony_ci   st->in_stride = istride_save;
105953a5a1b3Sopenharmony_ci   st->out_stride = ostride_save;
106053a5a1b3Sopenharmony_ci   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
106153a5a1b3Sopenharmony_ci}
106253a5a1b3Sopenharmony_ci
106353a5a1b3Sopenharmony_ciEXPORT int speex_resampler_process_interleaved_int(SpeexResamplerState *st, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
106453a5a1b3Sopenharmony_ci{
106553a5a1b3Sopenharmony_ci   spx_uint32_t i;
106653a5a1b3Sopenharmony_ci   int istride_save, ostride_save;
106753a5a1b3Sopenharmony_ci   spx_uint32_t bak_out_len = *out_len;
106853a5a1b3Sopenharmony_ci   spx_uint32_t bak_in_len = *in_len;
106953a5a1b3Sopenharmony_ci   istride_save = st->in_stride;
107053a5a1b3Sopenharmony_ci   ostride_save = st->out_stride;
107153a5a1b3Sopenharmony_ci   st->in_stride = st->out_stride = st->nb_channels;
107253a5a1b3Sopenharmony_ci   for (i=0;i<st->nb_channels;i++)
107353a5a1b3Sopenharmony_ci   {
107453a5a1b3Sopenharmony_ci      *out_len = bak_out_len;
107553a5a1b3Sopenharmony_ci      *in_len = bak_in_len;
107653a5a1b3Sopenharmony_ci      if (in != NULL)
107753a5a1b3Sopenharmony_ci         speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len);
107853a5a1b3Sopenharmony_ci      else
107953a5a1b3Sopenharmony_ci         speex_resampler_process_int(st, i, NULL, in_len, out+i, out_len);
108053a5a1b3Sopenharmony_ci   }
108153a5a1b3Sopenharmony_ci   st->in_stride = istride_save;
108253a5a1b3Sopenharmony_ci   st->out_stride = ostride_save;
108353a5a1b3Sopenharmony_ci   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
108453a5a1b3Sopenharmony_ci}
108553a5a1b3Sopenharmony_ci
108653a5a1b3Sopenharmony_ciEXPORT int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
108753a5a1b3Sopenharmony_ci{
108853a5a1b3Sopenharmony_ci   return speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate);
108953a5a1b3Sopenharmony_ci}
109053a5a1b3Sopenharmony_ci
109153a5a1b3Sopenharmony_ciEXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate)
109253a5a1b3Sopenharmony_ci{
109353a5a1b3Sopenharmony_ci   *in_rate = st->in_rate;
109453a5a1b3Sopenharmony_ci   *out_rate = st->out_rate;
109553a5a1b3Sopenharmony_ci}
109653a5a1b3Sopenharmony_ci
109753a5a1b3Sopenharmony_cistatic inline spx_uint32_t compute_gcd(spx_uint32_t a, spx_uint32_t b)
109853a5a1b3Sopenharmony_ci{
109953a5a1b3Sopenharmony_ci   while (b != 0)
110053a5a1b3Sopenharmony_ci   {
110153a5a1b3Sopenharmony_ci      spx_uint32_t temp = a;
110253a5a1b3Sopenharmony_ci
110353a5a1b3Sopenharmony_ci      a = b;
110453a5a1b3Sopenharmony_ci      b = temp % b;
110553a5a1b3Sopenharmony_ci   }
110653a5a1b3Sopenharmony_ci   return a;
110753a5a1b3Sopenharmony_ci}
110853a5a1b3Sopenharmony_ci
110953a5a1b3Sopenharmony_ciEXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate)
111053a5a1b3Sopenharmony_ci{
111153a5a1b3Sopenharmony_ci   spx_uint32_t fact;
111253a5a1b3Sopenharmony_ci   spx_uint32_t old_den;
111353a5a1b3Sopenharmony_ci   spx_uint32_t i;
111453a5a1b3Sopenharmony_ci
111553a5a1b3Sopenharmony_ci   if (ratio_num == 0 || ratio_den == 0)
111653a5a1b3Sopenharmony_ci      return RESAMPLER_ERR_INVALID_ARG;
111753a5a1b3Sopenharmony_ci
111853a5a1b3Sopenharmony_ci   if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den)
111953a5a1b3Sopenharmony_ci      return RESAMPLER_ERR_SUCCESS;
112053a5a1b3Sopenharmony_ci
112153a5a1b3Sopenharmony_ci   old_den = st->den_rate;
112253a5a1b3Sopenharmony_ci   st->in_rate = in_rate;
112353a5a1b3Sopenharmony_ci   st->out_rate = out_rate;
112453a5a1b3Sopenharmony_ci   st->num_rate = ratio_num;
112553a5a1b3Sopenharmony_ci   st->den_rate = ratio_den;
112653a5a1b3Sopenharmony_ci
112753a5a1b3Sopenharmony_ci   fact = compute_gcd(st->num_rate, st->den_rate);
112853a5a1b3Sopenharmony_ci
112953a5a1b3Sopenharmony_ci   st->num_rate /= fact;
113053a5a1b3Sopenharmony_ci   st->den_rate /= fact;
113153a5a1b3Sopenharmony_ci
113253a5a1b3Sopenharmony_ci   if (old_den > 0)
113353a5a1b3Sopenharmony_ci   {
113453a5a1b3Sopenharmony_ci      for (i=0;i<st->nb_channels;i++)
113553a5a1b3Sopenharmony_ci      {
113653a5a1b3Sopenharmony_ci         if (multiply_frac(&st->samp_frac_num[i],st->samp_frac_num[i],st->den_rate,old_den) != RESAMPLER_ERR_SUCCESS)
113753a5a1b3Sopenharmony_ci            return RESAMPLER_ERR_OVERFLOW;
113853a5a1b3Sopenharmony_ci         /* Safety net */
113953a5a1b3Sopenharmony_ci         if (st->samp_frac_num[i] >= st->den_rate)
114053a5a1b3Sopenharmony_ci            st->samp_frac_num[i] = st->den_rate-1;
114153a5a1b3Sopenharmony_ci      }
114253a5a1b3Sopenharmony_ci   }
114353a5a1b3Sopenharmony_ci
114453a5a1b3Sopenharmony_ci   if (st->initialised)
114553a5a1b3Sopenharmony_ci      return update_filter(st);
114653a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
114753a5a1b3Sopenharmony_ci}
114853a5a1b3Sopenharmony_ci
114953a5a1b3Sopenharmony_ciEXPORT void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den)
115053a5a1b3Sopenharmony_ci{
115153a5a1b3Sopenharmony_ci   *ratio_num = st->num_rate;
115253a5a1b3Sopenharmony_ci   *ratio_den = st->den_rate;
115353a5a1b3Sopenharmony_ci}
115453a5a1b3Sopenharmony_ci
115553a5a1b3Sopenharmony_ciEXPORT int speex_resampler_set_quality(SpeexResamplerState *st, int quality)
115653a5a1b3Sopenharmony_ci{
115753a5a1b3Sopenharmony_ci   if (quality > 10 || quality < 0)
115853a5a1b3Sopenharmony_ci      return RESAMPLER_ERR_INVALID_ARG;
115953a5a1b3Sopenharmony_ci   if (st->quality == quality)
116053a5a1b3Sopenharmony_ci      return RESAMPLER_ERR_SUCCESS;
116153a5a1b3Sopenharmony_ci   st->quality = quality;
116253a5a1b3Sopenharmony_ci   if (st->initialised)
116353a5a1b3Sopenharmony_ci      return update_filter(st);
116453a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
116553a5a1b3Sopenharmony_ci}
116653a5a1b3Sopenharmony_ci
116753a5a1b3Sopenharmony_ciEXPORT void speex_resampler_get_quality(SpeexResamplerState *st, int *quality)
116853a5a1b3Sopenharmony_ci{
116953a5a1b3Sopenharmony_ci   *quality = st->quality;
117053a5a1b3Sopenharmony_ci}
117153a5a1b3Sopenharmony_ci
117253a5a1b3Sopenharmony_ciEXPORT void speex_resampler_set_input_stride(SpeexResamplerState *st, spx_uint32_t stride)
117353a5a1b3Sopenharmony_ci{
117453a5a1b3Sopenharmony_ci   st->in_stride = stride;
117553a5a1b3Sopenharmony_ci}
117653a5a1b3Sopenharmony_ci
117753a5a1b3Sopenharmony_ciEXPORT void speex_resampler_get_input_stride(SpeexResamplerState *st, spx_uint32_t *stride)
117853a5a1b3Sopenharmony_ci{
117953a5a1b3Sopenharmony_ci   *stride = st->in_stride;
118053a5a1b3Sopenharmony_ci}
118153a5a1b3Sopenharmony_ci
118253a5a1b3Sopenharmony_ciEXPORT void speex_resampler_set_output_stride(SpeexResamplerState *st, spx_uint32_t stride)
118353a5a1b3Sopenharmony_ci{
118453a5a1b3Sopenharmony_ci   st->out_stride = stride;
118553a5a1b3Sopenharmony_ci}
118653a5a1b3Sopenharmony_ci
118753a5a1b3Sopenharmony_ciEXPORT void speex_resampler_get_output_stride(SpeexResamplerState *st, spx_uint32_t *stride)
118853a5a1b3Sopenharmony_ci{
118953a5a1b3Sopenharmony_ci   *stride = st->out_stride;
119053a5a1b3Sopenharmony_ci}
119153a5a1b3Sopenharmony_ci
119253a5a1b3Sopenharmony_ciEXPORT int speex_resampler_get_input_latency(SpeexResamplerState *st)
119353a5a1b3Sopenharmony_ci{
119453a5a1b3Sopenharmony_ci  return st->filt_len / 2;
119553a5a1b3Sopenharmony_ci}
119653a5a1b3Sopenharmony_ci
119753a5a1b3Sopenharmony_ciEXPORT int speex_resampler_get_output_latency(SpeexResamplerState *st)
119853a5a1b3Sopenharmony_ci{
119953a5a1b3Sopenharmony_ci  return ((st->filt_len / 2) * st->den_rate + (st->num_rate >> 1)) / st->num_rate;
120053a5a1b3Sopenharmony_ci}
120153a5a1b3Sopenharmony_ci
120253a5a1b3Sopenharmony_ciEXPORT int speex_resampler_skip_zeros(SpeexResamplerState *st)
120353a5a1b3Sopenharmony_ci{
120453a5a1b3Sopenharmony_ci   spx_uint32_t i;
120553a5a1b3Sopenharmony_ci   for (i=0;i<st->nb_channels;i++)
120653a5a1b3Sopenharmony_ci      st->last_sample[i] = st->filt_len/2;
120753a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
120853a5a1b3Sopenharmony_ci}
120953a5a1b3Sopenharmony_ci
121053a5a1b3Sopenharmony_ciEXPORT int speex_resampler_reset_mem(SpeexResamplerState *st)
121153a5a1b3Sopenharmony_ci{
121253a5a1b3Sopenharmony_ci   spx_uint32_t i;
121353a5a1b3Sopenharmony_ci   for (i=0;i<st->nb_channels;i++)
121453a5a1b3Sopenharmony_ci   {
121553a5a1b3Sopenharmony_ci      st->last_sample[i] = 0;
121653a5a1b3Sopenharmony_ci      st->magic_samples[i] = 0;
121753a5a1b3Sopenharmony_ci      st->samp_frac_num[i] = 0;
121853a5a1b3Sopenharmony_ci   }
121953a5a1b3Sopenharmony_ci   for (i=0;i<st->nb_channels*st->mem_alloc_size;i++)
122053a5a1b3Sopenharmony_ci      st->mem[i] = 0;
122153a5a1b3Sopenharmony_ci   return RESAMPLER_ERR_SUCCESS;
122253a5a1b3Sopenharmony_ci}
122353a5a1b3Sopenharmony_ci
122453a5a1b3Sopenharmony_ciEXPORT const char *speex_resampler_strerror(int err)
122553a5a1b3Sopenharmony_ci{
122653a5a1b3Sopenharmony_ci   switch (err)
122753a5a1b3Sopenharmony_ci   {
122853a5a1b3Sopenharmony_ci      case RESAMPLER_ERR_SUCCESS:
122953a5a1b3Sopenharmony_ci         return "Success.";
123053a5a1b3Sopenharmony_ci      case RESAMPLER_ERR_ALLOC_FAILED:
123153a5a1b3Sopenharmony_ci         return "Memory allocation failed.";
123253a5a1b3Sopenharmony_ci      case RESAMPLER_ERR_BAD_STATE:
123353a5a1b3Sopenharmony_ci         return "Bad resampler state.";
123453a5a1b3Sopenharmony_ci      case RESAMPLER_ERR_INVALID_ARG:
123553a5a1b3Sopenharmony_ci         return "Invalid argument.";
123653a5a1b3Sopenharmony_ci      case RESAMPLER_ERR_PTR_OVERLAP:
123753a5a1b3Sopenharmony_ci         return "Input and output buffers overlap.";
123853a5a1b3Sopenharmony_ci      default:
123953a5a1b3Sopenharmony_ci         return "Unknown error. Bad error code or strange version mismatch.";
124053a5a1b3Sopenharmony_ci   }
124153a5a1b3Sopenharmony_ci}
1242