66 static void *speex_alloc (
int size) {
return calloc(size,1);}
67 static void *speex_realloc (
void *ptr,
int size) {
return realloc(ptr, size);}
68 static void speex_free (
void *ptr) {
free(ptr);}
75 #include "os_support.h" 83 #define M_PI 3.14159265358979323846 87 #define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x))) 89 #define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x)))) 92 #define IMAX(a,b) ((a) > (b) ? (a) : (b)) 93 #define IMIN(a,b) ((a) < (b) ? (a) : (b)) 104 #include "resample_neon.h" 109 #define FIXED_STACK_ALLOC 8192 111 #define FIXED_STACK_ALLOC 1024 149 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076,
150 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014,
151 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601,
152 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014,
153 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490,
154 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546,
155 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178,
156 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947,
157 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058,
158 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438,
159 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734,
160 0.00001000, 0.00000000};
171 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446,
172 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347,
173 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962,
174 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451,
175 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739,
176 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000};
179 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200,
180 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126,
181 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272,
182 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758,
183 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490,
184 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000};
187 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003,
188 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565,
189 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561,
190 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058,
191 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600,
192 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000};
200 #define KAISER12 (&_KAISER12) 204 #define KAISER10 (&_KAISER10) 206 #define KAISER8 (&_KAISER8) 208 #define KAISER6 (&_KAISER6) 229 { 8, 4, 0.830f, 0.860f,
KAISER6 },
230 { 16, 4, 0.850f, 0.880f,
KAISER6 },
231 { 32, 4, 0.882f, 0.910f,
KAISER6 },
232 { 48, 8, 0.895f, 0.917f,
KAISER8 },
233 { 64, 8, 0.921f, 0.940f,
KAISER8 },
234 { 80, 16, 0.922f, 0.940f,
KAISER10},
235 { 96, 16, 0.940f, 0.945f,
KAISER10},
236 {128, 16, 0.950f, 0.950f,
KAISER10},
237 {160, 16, 0.960f, 0.960f,
KAISER10},
238 {192, 32, 0.968f, 0.968f,
KAISER12},
239 {256, 32, 0.975f, 0.975f,
KAISER12},
251 interp[3] = -0.1666666667*frac + 0.1666666667*(frac*frac*frac);
252 interp[2] = frac + 0.5*(frac*frac) - 0.5*(frac*frac*frac);
254 interp[0] = -0.3333333333*frac + 0.5*(frac*frac) - 0.1666666667*(frac*frac*frac);
256 interp[1] = 1.f-interp[3]-interp[2]-interp[0];
259 return interp[0]*func->
table[ind] + interp[1]*func->
table[ind+1] + interp[2]*func->
table[ind+2] + interp[3]*func->
table[ind+3];
264 int main(
int argc,
char **argv)
283 else if (fabs(x) > .5f*N)
296 else if (fabs(x) > .5*N)
315 interp[2] =
Q15_ONE-interp[0]-interp[1]-interp[3];
324 interp[0] = -0.16667f*frac + 0.16667f*frac*frac*frac;
325 interp[1] = frac + 0.5f*frac*frac - 0.5f*frac*frac*frac;
327 interp[3] = -0.33333f*frac + 0.5f*frac*frac - 0.16667f*frac*frac*frac;
329 interp[2] = 1.-interp[0]-interp[1]-interp[3];
346 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
348 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
351 #ifndef OVERRIDE_INNER_PRODUCT_SINGLE 354 for(j=0;j<N;j++) sum +=
MULT16_16(sinct[j], iptr[j]);
373 out[out_stride * out_sample++] = sum;
376 if (samp_frac_num >= den_rate)
404 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
406 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
409 #ifndef OVERRIDE_INNER_PRODUCT_DOUBLE 411 double accum[4] = {0,0,0,0};
414 accum[0] += sinct[j]*iptr[j];
415 accum[1] += sinct[j+1]*iptr[j+1];
416 accum[2] += sinct[j+2]*iptr[j+2];
417 accum[3] += sinct[j+3]*iptr[j+3];
419 sum = accum[0] + accum[1] + accum[2] + accum[3];
421 sum = inner_product_double(sinct, iptr, N);
424 out[out_stride * out_sample++] =
PSHR32(sum, 15);
427 if (samp_frac_num >= den_rate)
452 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
465 #ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE 485 out[out_stride * out_sample++] = sum;
488 if (samp_frac_num >= den_rate)
515 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
528 #ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE 530 double accum[4] = {0,0,0,0};
533 const double curr_in=iptr[j];
547 out[out_stride * out_sample++] =
PSHR32(sum,15);
550 if (samp_frac_num >= den_rate)
577 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
579 out[out_stride * out_sample++] = 0;
582 if (samp_frac_num >= den_rate)
596 spx_uint32_t old_length = st->
filt_len;
599 spx_uint32_t min_sinc_table_length;
600 spx_uint32_t min_alloc_size;
631 #ifdef RESAMPLE_FULL_SINC_TABLE 716 }
else if (st->
filt_len > old_length)
724 spx_uint32_t olen = old_length;
741 for (j=0;j<olen-1;j++)
755 }
else if (st->
filt_len < old_length)
794 if (quality > 10 || quality < 0)
822 st->
last_sample = (spx_int32_t*)speex_alloc(nb_channels*
sizeof(spx_int32_t));
823 st->
magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*
sizeof(spx_uint32_t));
824 st->
samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*
sizeof(spx_uint32_t));
870 out_sample = st->
resampler_ptr(st, channel_index, mem, in_len, out, out_len);
872 if (st->
last_sample[channel_index] < (spx_int32_t)*in_len)
874 *out_len = out_sample;
880 mem[j] = mem[j+ilen];
899 mem[N-1+i]=mem[N-1+i+tmp_in_len];
912 spx_uint32_t ilen = *in_len;
913 spx_uint32_t olen = *out_len;
915 const int filt_offs = st->
filt_len - 1;
922 while (ilen && olen) {
923 spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
924 spx_uint32_t ochunk = olen;
927 for(j=0;j<ichunk;++j)
928 x[j+filt_offs]=in[j*istride];
930 for(j=0;j<ichunk;++j)
938 in += ichunk * istride;
955 spx_uint32_t ilen = *in_len;
956 spx_uint32_t olen = *out_len;
970 while (ilen && olen) {
972 spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
973 spx_uint32_t ochunk = (olen > ylen) ? ylen : olen;
974 spx_uint32_t omagic = 0;
983 for(j=0;j<ichunk;++j)
987 x[j+st->
filt_len-1]=in[j*istride_save];
990 for(j=0;j<ichunk;++j)
1000 for (j=0;j<ochunk+omagic;++j)
1002 out[j*ostride_save] = ystack[j];
1004 out[j*ostride_save] =
WORD2INT(ystack[j]);
1009 out += (ochunk+omagic) * ostride_save;
1011 in += ichunk * istride_save;
1023 int istride_save, ostride_save;
1024 spx_uint32_t bak_out_len = *out_len;
1025 spx_uint32_t bak_in_len = *in_len;
1031 *out_len = bak_out_len;
1032 *in_len = bak_in_len;
1046 int istride_save, ostride_save;
1047 spx_uint32_t bak_out_len = *out_len;
1048 spx_uint32_t bak_in_len = *in_len;
1054 *out_len = bak_out_len;
1055 *in_len = bak_in_len;
1080 spx_uint32_t old_den;
1124 if (quality > 10 || quality < 0)
1198 return "Memory allocation failed.";
1200 return "Bad resampler state.";
1202 return "Invalid argument.";
1204 return "Input and output buffers overlap.";
1206 return "Unknown error. Bad error code or strange version mismatch.";
#define FIXED_STACK_ALLOC
spx_uint32_t * samp_frac_num
EXPORT int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
#define MULT16_16_P15(a, b)
int main(int argc, char *argv[])
EXPORT int speex_resampler_get_input_latency(SpeexResamplerState *st)
EXPORT int speex_resampler_skip_zeros(SpeexResamplerState *st)
#define SATURATE32PSHR(x, shift, a)
Resampler functions (SSE version)
EXPORT int speex_resampler_get_output_latency(SpeexResamplerState *st)
static float inner_product_single(const float *a, const float *b, unsigned int len)
EXPORT 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)
static const double kaiser10_table[36]
EXPORT void speex_resampler_get_quality(SpeexResamplerState *st, int *quality)
EXPORT SpeexResamplerState * speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
Create a new resampler with integer input and output rates.
EXPORT 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)
EXPORT int speex_resampler_set_quality(SpeexResamplerState *st, int quality)
EXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
static const double kaiser6_table[36]
static int update_filter(SpeexResamplerState *st)
EXPORT void speex_resampler_set_output_stride(SpeexResamplerState *st, spx_uint32_t stride)
resampler_basic_func resampler_ptr
static int speex_resampler_magic(SpeexResamplerState *st, spx_uint32_t channel_index, spx_word16_t **out, spx_uint32_t out_len)
EXPORT int speex_resampler_reset_mem(SpeexResamplerState *st)
EXPORT const char * speex_resampler_strerror(int err)
int(* resampler_basic_func)(SpeexResamplerState *, spx_uint32_t, const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *)
#define MULT16_32_Q15(a, b)
static 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)
spx_int32_t * last_sample
static const struct FuncDef _KAISER12
static const double kaiser8_table[36]
spx_word16_t * sinc_table
EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate)
#define ALLOC(var, size, type)
spx_uint32_t mem_alloc_size
static void cubic_coef(spx_word16_t x, spx_word16_t interp[4])
static const struct QualityMapping quality_map[11]
EXPORT void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den)
spx_uint32_t * magic_samples
static const struct FuncDef _KAISER8
const struct FuncDef * window_func
static 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)
static const double kaiser12_table[68]
spx_uint32_t sinc_table_length
Temporary memory allocation on stack.
static 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)
static const struct FuncDef _KAISER10
EXPORT void speex_resampler_get_output_stride(SpeexResamplerState *st, spx_uint32_t *stride)
#define QCONST16(x, bits)
EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
static float interpolate_product_single(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac)
EXPORT 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)
Various architecture definitions Speex.
float downsample_bandwidth
static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func)
EXPORT 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)
static double compute_func(float x, const struct FuncDef *func)
static 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)
EXPORT void speex_resampler_get_input_stride(SpeexResamplerState *st, spx_uint32_t *stride)
static const struct FuncDef _KAISER6
EXPORT 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)
EXPORT void speex_resampler_set_input_stride(SpeexResamplerState *st, spx_uint32_t stride)