You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1209 lines
43KB

  1. /* Copyright (C) 2007-2008 Jean-Marc Valin
  2. Copyright (C) 2008 Thorvald Natvig
  3. File: resample.c
  4. Arbitrary resampling code
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. The design goals of this code are:
  29. - Very fast algorithm
  30. - SIMD-friendly algorithm
  31. - Low memory requirement
  32. - Good *perceptual* quality (and not best SNR)
  33. Warning: This resampler is relatively new. Although I think I got rid of
  34. all the major bugs and I don't expect the API to change anymore, there
  35. may be something I've missed. So use with caution.
  36. This algorithm is based on this original resampling algorithm:
  37. Smith, Julius O. Digital Audio Resampling Home Page
  38. Center for Computer Research in Music and Acoustics (CCRMA),
  39. Stanford University, 2007.
  40. Web published at http://www-ccrma.stanford.edu/~jos/resample/.
  41. There is one main difference, though. This resampler uses cubic
  42. interpolation instead of linear interpolation in the above paper. This
  43. makes the table much smaller and makes it possible to compute that table
  44. on a per-stream basis. In turn, being able to tweak the table for each
  45. stream makes it possible to both reduce complexity on simple ratios
  46. (e.g. 2/3), and get rid of the rounding operations in the inner loop.
  47. The latter both reduces CPU time and makes the algorithm more SIMD-friendly.
  48. */
  49. #ifdef HAVE_CONFIG_H
  50. #include "config.h"
  51. #endif
  52. #ifdef OUTSIDE_SPEEX
  53. #include <stdlib.h>
  54. static void *speex_alloc (int size) {return calloc(size,1);}
  55. static void *speex_realloc (void *ptr, int size) {return realloc(ptr, size);}
  56. static void speex_free (void *ptr) {free(ptr);}
  57. #include "speex_resampler.h"
  58. #include "arch.h"
  59. #else /* OUTSIDE_SPEEX */
  60. #include "speex/speex_resampler.h"
  61. #include "arch.h"
  62. #include "os_support.h"
  63. #endif /* OUTSIDE_SPEEX */
  64. #include "stack_alloc.h"
  65. #include <math.h>
  66. #include <limits.h>
  67. #ifndef M_PI
  68. #define M_PI 3.14159265358979323846
  69. #endif
  70. #ifdef FIXED_POINT
  71. #define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x)))
  72. #else
  73. #define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x))))
  74. #endif
  75. #define IMAX(a,b) ((a) > (b) ? (a) : (b))
  76. #define IMIN(a,b) ((a) < (b) ? (a) : (b))
  77. #ifndef NULL
  78. #define NULL 0
  79. #endif
  80. #ifdef _USE_SSE
  81. #include "resample_sse.h"
  82. #endif
  83. #ifdef _USE_NEON
  84. #include "resample_neon.h"
  85. #endif
  86. /* Numer of elements to allocate on the stack */
  87. #ifdef VAR_ARRAYS
  88. #define FIXED_STACK_ALLOC 8192
  89. #else
  90. #define FIXED_STACK_ALLOC 1024
  91. #endif
  92. typedef int (*resampler_basic_func)(SpeexResamplerState *, spx_uint32_t , const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *);
  93. struct SpeexResamplerState_ {
  94. spx_uint32_t in_rate;
  95. spx_uint32_t out_rate;
  96. spx_uint32_t num_rate;
  97. spx_uint32_t den_rate;
  98. int quality;
  99. spx_uint32_t nb_channels;
  100. spx_uint32_t filt_len;
  101. spx_uint32_t mem_alloc_size;
  102. spx_uint32_t buffer_size;
  103. int int_advance;
  104. int frac_advance;
  105. float cutoff;
  106. spx_uint32_t oversample;
  107. int initialised;
  108. int started;
  109. /* These are per-channel */
  110. spx_int32_t *last_sample;
  111. spx_uint32_t *samp_frac_num;
  112. spx_uint32_t *magic_samples;
  113. spx_word16_t *mem;
  114. spx_word16_t *sinc_table;
  115. spx_uint32_t sinc_table_length;
  116. resampler_basic_func resampler_ptr;
  117. int in_stride;
  118. int out_stride;
  119. } ;
  120. static const double kaiser12_table[68] = {
  121. 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076,
  122. 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014,
  123. 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601,
  124. 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014,
  125. 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490,
  126. 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546,
  127. 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178,
  128. 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947,
  129. 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058,
  130. 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438,
  131. 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734,
  132. 0.00001000, 0.00000000};
  133. /*
  134. static const double kaiser12_table[36] = {
  135. 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741,
  136. 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762,
  137. 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274,
  138. 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466,
  139. 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291,
  140. 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000};
  141. */
  142. static const double kaiser10_table[36] = {
  143. 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446,
  144. 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347,
  145. 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962,
  146. 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451,
  147. 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739,
  148. 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000};
  149. static const double kaiser8_table[36] = {
  150. 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200,
  151. 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126,
  152. 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272,
  153. 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758,
  154. 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490,
  155. 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000};
  156. static const double kaiser6_table[36] = {
  157. 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003,
  158. 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565,
  159. 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561,
  160. 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058,
  161. 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600,
  162. 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000};
  163. struct FuncDef {
  164. const double *table;
  165. int oversample;
  166. };
  167. static const struct FuncDef _KAISER12 = {kaiser12_table, 64};
  168. #define KAISER12 (&_KAISER12)
  169. /*static struct FuncDef _KAISER12 = {kaiser12_table, 32};
  170. #define KAISER12 (&_KAISER12)*/
  171. static const struct FuncDef _KAISER10 = {kaiser10_table, 32};
  172. #define KAISER10 (&_KAISER10)
  173. static const struct FuncDef _KAISER8 = {kaiser8_table, 32};
  174. #define KAISER8 (&_KAISER8)
  175. static const struct FuncDef _KAISER6 = {kaiser6_table, 32};
  176. #define KAISER6 (&_KAISER6)
  177. struct QualityMapping {
  178. int base_length;
  179. int oversample;
  180. float downsample_bandwidth;
  181. float upsample_bandwidth;
  182. const struct FuncDef *window_func;
  183. };
  184. /* This table maps conversion quality to internal parameters. There are two
  185. reasons that explain why the up-sampling bandwidth is larger than the
  186. down-sampling bandwidth:
  187. 1) When up-sampling, we can assume that the spectrum is already attenuated
  188. close to the Nyquist rate (from an A/D or a previous resampling filter)
  189. 2) Any aliasing that occurs very close to the Nyquist rate will be masked
  190. by the sinusoids/noise just below the Nyquist rate (guaranteed only for
  191. up-sampling).
  192. */
  193. static const struct QualityMapping quality_map[11] = {
  194. { 8, 4, 0.830f, 0.860f, KAISER6 }, /* Q0 */
  195. { 16, 4, 0.850f, 0.880f, KAISER6 }, /* Q1 */
  196. { 32, 4, 0.882f, 0.910f, KAISER6 }, /* Q2 */ /* 82.3% cutoff ( ~60 dB stop) 6 */
  197. { 48, 8, 0.895f, 0.917f, KAISER8 }, /* Q3 */ /* 84.9% cutoff ( ~80 dB stop) 8 */
  198. { 64, 8, 0.921f, 0.940f, KAISER8 }, /* Q4 */ /* 88.7% cutoff ( ~80 dB stop) 8 */
  199. { 80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 */ /* 89.1% cutoff (~100 dB stop) 10 */
  200. { 96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 */ /* 91.5% cutoff (~100 dB stop) 10 */
  201. {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 */ /* 93.1% cutoff (~100 dB stop) 10 */
  202. {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 */ /* 94.5% cutoff (~100 dB stop) 10 */
  203. {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 */ /* 95.5% cutoff (~100 dB stop) 10 */
  204. {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 */ /* 96.6% cutoff (~100 dB stop) 10 */
  205. };
  206. /*8,24,40,56,80,104,128,160,200,256,320*/
  207. static double compute_func(float x, const struct FuncDef *func)
  208. {
  209. float y, frac;
  210. double interp[4];
  211. int ind;
  212. y = x*func->oversample;
  213. ind = (int)floor(y);
  214. frac = (y-ind);
  215. /* CSE with handle the repeated powers */
  216. interp[3] = -0.1666666667*frac + 0.1666666667*(frac*frac*frac);
  217. interp[2] = frac + 0.5*(frac*frac) - 0.5*(frac*frac*frac);
  218. /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
  219. interp[0] = -0.3333333333*frac + 0.5*(frac*frac) - 0.1666666667*(frac*frac*frac);
  220. /* Just to make sure we don't have rounding problems */
  221. interp[1] = 1.f-interp[3]-interp[2]-interp[0];
  222. /*sum = frac*accum[1] + (1-frac)*accum[2];*/
  223. return interp[0]*func->table[ind] + interp[1]*func->table[ind+1] + interp[2]*func->table[ind+2] + interp[3]*func->table[ind+3];
  224. }
  225. #if 0
  226. #include <stdio.h>
  227. int main(int argc, char **argv)
  228. {
  229. int i;
  230. for (i=0;i<256;i++)
  231. {
  232. printf ("%f\n", compute_func(i/256., KAISER12));
  233. }
  234. return 0;
  235. }
  236. #endif
  237. #ifdef FIXED_POINT
  238. /* The slow way of computing a sinc for the table. Should improve that some day */
  239. static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func)
  240. {
  241. /*fprintf (stderr, "%f ", x);*/
  242. float xx = x * cutoff;
  243. if (fabs(x)<1e-6f)
  244. return WORD2INT(32768.*cutoff);
  245. else if (fabs(x) > .5f*N)
  246. return 0;
  247. /*FIXME: Can it really be any slower than this? */
  248. return WORD2INT(32768.*cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func));
  249. }
  250. #else
  251. /* The slow way of computing a sinc for the table. Should improve that some day */
  252. static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func)
  253. {
  254. /*fprintf (stderr, "%f ", x);*/
  255. float xx = x * cutoff;
  256. if (fabs(x)<1e-6)
  257. return cutoff;
  258. else if (fabs(x) > .5*N)
  259. return 0;
  260. /*FIXME: Can it really be any slower than this? */
  261. return cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func);
  262. }
  263. #endif
  264. #ifdef FIXED_POINT
  265. static void cubic_coef(spx_word16_t x, spx_word16_t interp[4])
  266. {
  267. /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
  268. but I know it's MMSE-optimal on a sinc */
  269. spx_word16_t x2, x3;
  270. x2 = MULT16_16_P15(x, x);
  271. x3 = MULT16_16_P15(x, x2);
  272. interp[0] = PSHR32(MULT16_16(QCONST16(-0.16667f, 15),x) + MULT16_16(QCONST16(0.16667f, 15),x3),15);
  273. interp[1] = EXTRACT16(EXTEND32(x) + SHR32(SUB32(EXTEND32(x2),EXTEND32(x3)),1));
  274. interp[3] = PSHR32(MULT16_16(QCONST16(-0.33333f, 15),x) + MULT16_16(QCONST16(.5f,15),x2) - MULT16_16(QCONST16(0.16667f, 15),x3),15);
  275. /* Just to make sure we don't have rounding problems */
  276. interp[2] = Q15_ONE-interp[0]-interp[1]-interp[3];
  277. if (interp[2]<32767)
  278. interp[2]+=1;
  279. }
  280. #else
  281. static void cubic_coef(spx_word16_t frac, spx_word16_t interp[4])
  282. {
  283. /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
  284. but I know it's MMSE-optimal on a sinc */
  285. interp[0] = -0.16667f*frac + 0.16667f*frac*frac*frac;
  286. interp[1] = frac + 0.5f*frac*frac - 0.5f*frac*frac*frac;
  287. /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
  288. interp[3] = -0.33333f*frac + 0.5f*frac*frac - 0.16667f*frac*frac*frac;
  289. /* Just to make sure we don't have rounding problems */
  290. interp[2] = 1.-interp[0]-interp[1]-interp[3];
  291. }
  292. #endif
  293. 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)
  294. {
  295. const int N = st->filt_len;
  296. int out_sample = 0;
  297. int last_sample = st->last_sample[channel_index];
  298. spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
  299. const spx_word16_t *sinc_table = st->sinc_table;
  300. const int out_stride = st->out_stride;
  301. const int int_advance = st->int_advance;
  302. const int frac_advance = st->frac_advance;
  303. const spx_uint32_t den_rate = st->den_rate;
  304. spx_word32_t sum;
  305. while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
  306. {
  307. const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
  308. const spx_word16_t *iptr = & in[last_sample];
  309. #ifndef OVERRIDE_INNER_PRODUCT_SINGLE
  310. int j;
  311. sum = 0;
  312. for(j=0;j<N;j++) sum += MULT16_16(sinct[j], iptr[j]);
  313. /* This code is slower on most DSPs which have only 2 accumulators.
  314. Plus this this forces truncation to 32 bits and you lose the HW guard bits.
  315. I think we can trust the compiler and let it vectorize and/or unroll itself.
  316. spx_word32_t accum[4] = {0,0,0,0};
  317. for(j=0;j<N;j+=4) {
  318. accum[0] += MULT16_16(sinct[j], iptr[j]);
  319. accum[1] += MULT16_16(sinct[j+1], iptr[j+1]);
  320. accum[2] += MULT16_16(sinct[j+2], iptr[j+2]);
  321. accum[3] += MULT16_16(sinct[j+3], iptr[j+3]);
  322. }
  323. sum = accum[0] + accum[1] + accum[2] + accum[3];
  324. */
  325. sum = SATURATE32PSHR(sum, 15, 32767);
  326. #else
  327. sum = inner_product_single(sinct, iptr, N);
  328. #endif
  329. out[out_stride * out_sample++] = sum;
  330. last_sample += int_advance;
  331. samp_frac_num += frac_advance;
  332. if (samp_frac_num >= den_rate)
  333. {
  334. samp_frac_num -= den_rate;
  335. last_sample++;
  336. }
  337. }
  338. st->last_sample[channel_index] = last_sample;
  339. st->samp_frac_num[channel_index] = samp_frac_num;
  340. return out_sample;
  341. }
  342. #ifdef FIXED_POINT
  343. #else
  344. /* This is the same as the previous function, except with a double-precision accumulator */
  345. static 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)
  346. {
  347. const int N = st->filt_len;
  348. int out_sample = 0;
  349. int last_sample = st->last_sample[channel_index];
  350. spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
  351. const spx_word16_t *sinc_table = st->sinc_table;
  352. const int out_stride = st->out_stride;
  353. const int int_advance = st->int_advance;
  354. const int frac_advance = st->frac_advance;
  355. const spx_uint32_t den_rate = st->den_rate;
  356. double sum;
  357. while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
  358. {
  359. const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
  360. const spx_word16_t *iptr = & in[last_sample];
  361. #ifndef OVERRIDE_INNER_PRODUCT_DOUBLE
  362. int j;
  363. double accum[4] = {0,0,0,0};
  364. for(j=0;j<N;j+=4) {
  365. accum[0] += sinct[j]*iptr[j];
  366. accum[1] += sinct[j+1]*iptr[j+1];
  367. accum[2] += sinct[j+2]*iptr[j+2];
  368. accum[3] += sinct[j+3]*iptr[j+3];
  369. }
  370. sum = accum[0] + accum[1] + accum[2] + accum[3];
  371. #else
  372. sum = inner_product_double(sinct, iptr, N);
  373. #endif
  374. out[out_stride * out_sample++] = PSHR32(sum, 15);
  375. last_sample += int_advance;
  376. samp_frac_num += frac_advance;
  377. if (samp_frac_num >= den_rate)
  378. {
  379. samp_frac_num -= den_rate;
  380. last_sample++;
  381. }
  382. }
  383. st->last_sample[channel_index] = last_sample;
  384. st->samp_frac_num[channel_index] = samp_frac_num;
  385. return out_sample;
  386. }
  387. #endif
  388. 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)
  389. {
  390. const int N = st->filt_len;
  391. int out_sample = 0;
  392. int last_sample = st->last_sample[channel_index];
  393. spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
  394. const int out_stride = st->out_stride;
  395. const int int_advance = st->int_advance;
  396. const int frac_advance = st->frac_advance;
  397. const spx_uint32_t den_rate = st->den_rate;
  398. spx_word32_t sum;
  399. while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
  400. {
  401. const spx_word16_t *iptr = & in[last_sample];
  402. const int offset = samp_frac_num*st->oversample/st->den_rate;
  403. #ifdef FIXED_POINT
  404. const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
  405. #else
  406. const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
  407. #endif
  408. spx_word16_t interp[4];
  409. #ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
  410. int j;
  411. spx_word32_t accum[4] = {0,0,0,0};
  412. for(j=0;j<N;j++) {
  413. const spx_word16_t curr_in=iptr[j];
  414. accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
  415. accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
  416. accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
  417. accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
  418. }
  419. cubic_coef(frac, interp);
  420. 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));
  421. sum = SATURATE32PSHR(sum, 15, 32767);
  422. #else
  423. cubic_coef(frac, interp);
  424. sum = interpolate_product_single(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
  425. #endif
  426. out[out_stride * out_sample++] = sum;
  427. last_sample += int_advance;
  428. samp_frac_num += frac_advance;
  429. if (samp_frac_num >= den_rate)
  430. {
  431. samp_frac_num -= den_rate;
  432. last_sample++;
  433. }
  434. }
  435. st->last_sample[channel_index] = last_sample;
  436. st->samp_frac_num[channel_index] = samp_frac_num;
  437. return out_sample;
  438. }
  439. #ifdef FIXED_POINT
  440. #else
  441. /* This is the same as the previous function, except with a double-precision accumulator */
  442. static 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)
  443. {
  444. const int N = st->filt_len;
  445. int out_sample = 0;
  446. int last_sample = st->last_sample[channel_index];
  447. spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
  448. const int out_stride = st->out_stride;
  449. const int int_advance = st->int_advance;
  450. const int frac_advance = st->frac_advance;
  451. const spx_uint32_t den_rate = st->den_rate;
  452. spx_word32_t sum;
  453. while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
  454. {
  455. const spx_word16_t *iptr = & in[last_sample];
  456. const int offset = samp_frac_num*st->oversample/st->den_rate;
  457. #ifdef FIXED_POINT
  458. const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
  459. #else
  460. const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
  461. #endif
  462. spx_word16_t interp[4];
  463. #ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
  464. int j;
  465. double accum[4] = {0,0,0,0};
  466. for(j=0;j<N;j++) {
  467. const double curr_in=iptr[j];
  468. accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
  469. accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
  470. accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
  471. accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
  472. }
  473. cubic_coef(frac, interp);
  474. 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]);
  475. #else
  476. cubic_coef(frac, interp);
  477. sum = interpolate_product_double(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
  478. #endif
  479. out[out_stride * out_sample++] = PSHR32(sum,15);
  480. last_sample += int_advance;
  481. samp_frac_num += frac_advance;
  482. if (samp_frac_num >= den_rate)
  483. {
  484. samp_frac_num -= den_rate;
  485. last_sample++;
  486. }
  487. }
  488. st->last_sample[channel_index] = last_sample;
  489. st->samp_frac_num[channel_index] = samp_frac_num;
  490. return out_sample;
  491. }
  492. #endif
  493. /* This resampler is used to produce zero output in situations where memory
  494. for the filter could not be allocated. The expected numbers of input and
  495. output samples are still processed so that callers failing to check error
  496. codes are not surprised, possibly getting into infinite loops. */
  497. 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)
  498. {
  499. int out_sample = 0;
  500. int last_sample = st->last_sample[channel_index];
  501. spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
  502. const int out_stride = st->out_stride;
  503. const int int_advance = st->int_advance;
  504. const int frac_advance = st->frac_advance;
  505. const spx_uint32_t den_rate = st->den_rate;
  506. while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
  507. {
  508. out[out_stride * out_sample++] = 0;
  509. last_sample += int_advance;
  510. samp_frac_num += frac_advance;
  511. if (samp_frac_num >= den_rate)
  512. {
  513. samp_frac_num -= den_rate;
  514. last_sample++;
  515. }
  516. }
  517. st->last_sample[channel_index] = last_sample;
  518. st->samp_frac_num[channel_index] = samp_frac_num;
  519. return out_sample;
  520. }
  521. static int update_filter(SpeexResamplerState *st)
  522. {
  523. spx_uint32_t old_length = st->filt_len;
  524. spx_uint32_t old_alloc_size = st->mem_alloc_size;
  525. int use_direct;
  526. spx_uint32_t min_sinc_table_length;
  527. spx_uint32_t min_alloc_size;
  528. st->int_advance = st->num_rate/st->den_rate;
  529. st->frac_advance = st->num_rate%st->den_rate;
  530. st->oversample = quality_map[st->quality].oversample;
  531. st->filt_len = quality_map[st->quality].base_length;
  532. if (st->num_rate > st->den_rate)
  533. {
  534. /* down-sampling */
  535. st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate;
  536. /* FIXME: divide the numerator and denominator by a certain amount if they're too large */
  537. st->filt_len = st->filt_len*st->num_rate / st->den_rate;
  538. /* Round up to make sure we have a multiple of 8 for SSE */
  539. st->filt_len = ((st->filt_len-1)&(~0x7))+8;
  540. if (2*st->den_rate < st->num_rate)
  541. st->oversample >>= 1;
  542. if (4*st->den_rate < st->num_rate)
  543. st->oversample >>= 1;
  544. if (8*st->den_rate < st->num_rate)
  545. st->oversample >>= 1;
  546. if (16*st->den_rate < st->num_rate)
  547. st->oversample >>= 1;
  548. if (st->oversample < 1)
  549. st->oversample = 1;
  550. } else {
  551. /* up-sampling */
  552. st->cutoff = quality_map[st->quality].upsample_bandwidth;
  553. }
  554. /* Choose the resampling type that requires the least amount of memory */
  555. #ifdef RESAMPLE_FULL_SINC_TABLE
  556. use_direct = 1;
  557. if (INT_MAX/sizeof(spx_word16_t)/st->den_rate < st->filt_len)
  558. goto fail;
  559. #else
  560. use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8
  561. && INT_MAX/sizeof(spx_word16_t)/st->den_rate >= st->filt_len;
  562. #endif
  563. if (use_direct)
  564. {
  565. min_sinc_table_length = st->filt_len*st->den_rate;
  566. } else {
  567. if ((INT_MAX/sizeof(spx_word16_t)-8)/st->oversample < st->filt_len)
  568. goto fail;
  569. min_sinc_table_length = st->filt_len*st->oversample+8;
  570. }
  571. if (st->sinc_table_length < min_sinc_table_length)
  572. {
  573. spx_word16_t *sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,min_sinc_table_length*sizeof(spx_word16_t));
  574. if (!sinc_table)
  575. goto fail;
  576. st->sinc_table = sinc_table;
  577. st->sinc_table_length = min_sinc_table_length;
  578. }
  579. if (use_direct)
  580. {
  581. spx_uint32_t i;
  582. for (i=0;i<st->den_rate;i++)
  583. {
  584. spx_int32_t j;
  585. for (j=0;j<st->filt_len;j++)
  586. {
  587. 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);
  588. }
  589. }
  590. #ifdef FIXED_POINT
  591. st->resampler_ptr = resampler_basic_direct_single;
  592. #else
  593. if (st->quality>8)
  594. st->resampler_ptr = resampler_basic_direct_double;
  595. else
  596. st->resampler_ptr = resampler_basic_direct_single;
  597. #endif
  598. /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);*/
  599. } else {
  600. spx_int32_t i;
  601. for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++)
  602. 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);
  603. #ifdef FIXED_POINT
  604. st->resampler_ptr = resampler_basic_interpolate_single;
  605. #else
  606. if (st->quality>8)
  607. st->resampler_ptr = resampler_basic_interpolate_double;
  608. else
  609. st->resampler_ptr = resampler_basic_interpolate_single;
  610. #endif
  611. /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/
  612. }
  613. /* Here's the place where we update the filter memory to take into account
  614. the change in filter length. It's probably the messiest part of the code
  615. due to handling of lots of corner cases. */
  616. /* Adding buffer_size to filt_len won't overflow here because filt_len
  617. could be multiplied by sizeof(spx_word16_t) above. */
  618. min_alloc_size = st->filt_len-1 + st->buffer_size;
  619. if (min_alloc_size > st->mem_alloc_size)
  620. {
  621. spx_word16_t *mem;
  622. if (INT_MAX/sizeof(spx_word16_t)/st->nb_channels < min_alloc_size)
  623. goto fail;
  624. else if (!(mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(*mem))))
  625. goto fail;
  626. st->mem = mem;
  627. st->mem_alloc_size = min_alloc_size;
  628. }
  629. if (!st->started)
  630. {
  631. spx_uint32_t i;
  632. for (i=0;i<st->nb_channels*st->mem_alloc_size;i++)
  633. st->mem[i] = 0;
  634. /*speex_warning("reinit filter");*/
  635. } else if (st->filt_len > old_length)
  636. {
  637. spx_uint32_t i;
  638. /* Increase the filter length */
  639. /*speex_warning("increase filter size");*/
  640. for (i=st->nb_channels;i--;)
  641. {
  642. spx_uint32_t j;
  643. spx_uint32_t olen = old_length;
  644. /*if (st->magic_samples[i])*/
  645. {
  646. /* Try and remove the magic samples as if nothing had happened */
  647. /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */
  648. olen = old_length + 2*st->magic_samples[i];
  649. for (j=old_length-1+st->magic_samples[i];j--;)
  650. st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]] = st->mem[i*old_alloc_size+j];
  651. for (j=0;j<st->magic_samples[i];j++)
  652. st->mem[i*st->mem_alloc_size+j] = 0;
  653. st->magic_samples[i] = 0;
  654. }
  655. if (st->filt_len > olen)
  656. {
  657. /* If the new filter length is still bigger than the "augmented" length */
  658. /* Copy data going backward */
  659. for (j=0;j<olen-1;j++)
  660. st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = st->mem[i*st->mem_alloc_size+(olen-2-j)];
  661. /* Then put zeros for lack of anything better */
  662. for (;j<st->filt_len-1;j++)
  663. st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = 0;
  664. /* Adjust last_sample */
  665. st->last_sample[i] += (st->filt_len - olen)/2;
  666. } else {
  667. /* Put back some of the magic! */
  668. st->magic_samples[i] = (olen - st->filt_len)/2;
  669. for (j=0;j<st->filt_len-1+st->magic_samples[i];j++)
  670. st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
  671. }
  672. }
  673. } else if (st->filt_len < old_length)
  674. {
  675. spx_uint32_t i;
  676. /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic"
  677. samples so they can be used directly as input the next time(s) */
  678. for (i=0;i<st->nb_channels;i++)
  679. {
  680. spx_uint32_t j;
  681. spx_uint32_t old_magic = st->magic_samples[i];
  682. st->magic_samples[i] = (old_length - st->filt_len)/2;
  683. /* We must copy some of the memory that's no longer used */
  684. /* Copy data going backward */
  685. for (j=0;j<st->filt_len-1+st->magic_samples[i]+old_magic;j++)
  686. st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
  687. st->magic_samples[i] += old_magic;
  688. }
  689. }
  690. return RESAMPLER_ERR_SUCCESS;
  691. fail:
  692. st->resampler_ptr = resampler_basic_zero;
  693. /* st->mem may still contain consumed input samples for the filter.
  694. Restore filt_len so that filt_len - 1 still points to the position after
  695. the last of these samples. */
  696. st->filt_len = old_length;
  697. return RESAMPLER_ERR_ALLOC_FAILED;
  698. }
  699. EXPORT SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
  700. {
  701. return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err);
  702. }
  703. 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)
  704. {
  705. spx_uint32_t i;
  706. SpeexResamplerState *st;
  707. int filter_err;
  708. if (quality > 10 || quality < 0)
  709. {
  710. if (err)
  711. *err = RESAMPLER_ERR_INVALID_ARG;
  712. return NULL;
  713. }
  714. st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
  715. st->initialised = 0;
  716. st->started = 0;
  717. st->in_rate = 0;
  718. st->out_rate = 0;
  719. st->num_rate = 0;
  720. st->den_rate = 0;
  721. st->quality = -1;
  722. st->sinc_table_length = 0;
  723. st->mem_alloc_size = 0;
  724. st->filt_len = 0;
  725. st->mem = 0;
  726. st->resampler_ptr = 0;
  727. st->cutoff = 1.f;
  728. st->nb_channels = nb_channels;
  729. st->in_stride = 1;
  730. st->out_stride = 1;
  731. st->buffer_size = 160;
  732. /* Per channel data */
  733. st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t));
  734. st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
  735. st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
  736. for (i=0;i<nb_channels;i++)
  737. {
  738. st->last_sample[i] = 0;
  739. st->magic_samples[i] = 0;
  740. st->samp_frac_num[i] = 0;
  741. }
  742. speex_resampler_set_quality(st, quality);
  743. speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate);
  744. filter_err = update_filter(st);
  745. if (filter_err == RESAMPLER_ERR_SUCCESS)
  746. {
  747. st->initialised = 1;
  748. } else {
  749. speex_resampler_destroy(st);
  750. st = NULL;
  751. }
  752. if (err)
  753. *err = filter_err;
  754. return st;
  755. }
  756. EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
  757. {
  758. speex_free(st->mem);
  759. speex_free(st->sinc_table);
  760. speex_free(st->last_sample);
  761. speex_free(st->magic_samples);
  762. speex_free(st->samp_frac_num);
  763. speex_free(st);
  764. }
  765. 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)
  766. {
  767. int j=0;
  768. const int N = st->filt_len;
  769. int out_sample = 0;
  770. spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
  771. spx_uint32_t ilen;
  772. st->started = 1;
  773. /* Call the right resampler through the function ptr */
  774. out_sample = st->resampler_ptr(st, channel_index, mem, in_len, out, out_len);
  775. if (st->last_sample[channel_index] < (spx_int32_t)*in_len)
  776. *in_len = st->last_sample[channel_index];
  777. *out_len = out_sample;
  778. st->last_sample[channel_index] -= *in_len;
  779. ilen = *in_len;
  780. for(j=0;j<N-1;++j)
  781. mem[j] = mem[j+ilen];
  782. return RESAMPLER_ERR_SUCCESS;
  783. }
  784. static int speex_resampler_magic(SpeexResamplerState *st, spx_uint32_t channel_index, spx_word16_t **out, spx_uint32_t out_len) {
  785. spx_uint32_t tmp_in_len = st->magic_samples[channel_index];
  786. spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
  787. const int N = st->filt_len;
  788. speex_resampler_process_native(st, channel_index, &tmp_in_len, *out, &out_len);
  789. st->magic_samples[channel_index] -= tmp_in_len;
  790. /* If we couldn't process all "magic" input samples, save the rest for next time */
  791. if (st->magic_samples[channel_index])
  792. {
  793. spx_uint32_t i;
  794. for (i=0;i<st->magic_samples[channel_index];i++)
  795. mem[N-1+i]=mem[N-1+i+tmp_in_len];
  796. }
  797. *out += out_len*st->out_stride;
  798. return out_len;
  799. }
  800. #ifdef FIXED_POINT
  801. 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)
  802. #else
  803. 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)
  804. #endif
  805. {
  806. int j;
  807. spx_uint32_t ilen = *in_len;
  808. spx_uint32_t olen = *out_len;
  809. spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
  810. const int filt_offs = st->filt_len - 1;
  811. const spx_uint32_t xlen = st->mem_alloc_size - filt_offs;
  812. const int istride = st->in_stride;
  813. if (st->magic_samples[channel_index])
  814. olen -= speex_resampler_magic(st, channel_index, &out, olen);
  815. if (! st->magic_samples[channel_index]) {
  816. while (ilen && olen) {
  817. spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
  818. spx_uint32_t ochunk = olen;
  819. if (in) {
  820. for(j=0;j<ichunk;++j)
  821. x[j+filt_offs]=in[j*istride];
  822. } else {
  823. for(j=0;j<ichunk;++j)
  824. x[j+filt_offs]=0;
  825. }
  826. speex_resampler_process_native(st, channel_index, &ichunk, out, &ochunk);
  827. ilen -= ichunk;
  828. olen -= ochunk;
  829. out += ochunk * st->out_stride;
  830. if (in)
  831. in += ichunk * istride;
  832. }
  833. }
  834. *in_len -= ilen;
  835. *out_len -= olen;
  836. return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
  837. }
  838. #ifdef FIXED_POINT
  839. 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)
  840. #else
  841. 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)
  842. #endif
  843. {
  844. int j;
  845. const int istride_save = st->in_stride;
  846. const int ostride_save = st->out_stride;
  847. spx_uint32_t ilen = *in_len;
  848. spx_uint32_t olen = *out_len;
  849. spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
  850. const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1);
  851. #ifdef VAR_ARRAYS
  852. const unsigned int ylen = (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC;
  853. VARDECL(spx_word16_t *ystack);
  854. ALLOC(ystack, ylen, spx_word16_t);
  855. #else
  856. const unsigned int ylen = FIXED_STACK_ALLOC;
  857. spx_word16_t ystack[FIXED_STACK_ALLOC];
  858. #endif
  859. st->out_stride = 1;
  860. while (ilen && olen) {
  861. spx_word16_t *y = ystack;
  862. spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
  863. spx_uint32_t ochunk = (olen > ylen) ? ylen : olen;
  864. spx_uint32_t omagic = 0;
  865. if (st->magic_samples[channel_index]) {
  866. omagic = speex_resampler_magic(st, channel_index, &y, ochunk);
  867. ochunk -= omagic;
  868. olen -= omagic;
  869. }
  870. if (! st->magic_samples[channel_index]) {
  871. if (in) {
  872. for(j=0;j<ichunk;++j)
  873. #ifdef FIXED_POINT
  874. x[j+st->filt_len-1]=WORD2INT(in[j*istride_save]);
  875. #else
  876. x[j+st->filt_len-1]=in[j*istride_save];
  877. #endif
  878. } else {
  879. for(j=0;j<ichunk;++j)
  880. x[j+st->filt_len-1]=0;
  881. }
  882. speex_resampler_process_native(st, channel_index, &ichunk, y, &ochunk);
  883. } else {
  884. ichunk = 0;
  885. ochunk = 0;
  886. }
  887. for (j=0;j<ochunk+omagic;++j)
  888. #ifdef FIXED_POINT
  889. out[j*ostride_save] = ystack[j];
  890. #else
  891. out[j*ostride_save] = WORD2INT(ystack[j]);
  892. #endif
  893. ilen -= ichunk;
  894. olen -= ochunk;
  895. out += (ochunk+omagic) * ostride_save;
  896. if (in)
  897. in += ichunk * istride_save;
  898. }
  899. st->out_stride = ostride_save;
  900. *in_len -= ilen;
  901. *out_len -= olen;
  902. return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
  903. }
  904. EXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
  905. {
  906. spx_uint32_t i;
  907. int istride_save, ostride_save;
  908. spx_uint32_t bak_out_len = *out_len;
  909. spx_uint32_t bak_in_len = *in_len;
  910. istride_save = st->in_stride;
  911. ostride_save = st->out_stride;
  912. st->in_stride = st->out_stride = st->nb_channels;
  913. for (i=0;i<st->nb_channels;i++)
  914. {
  915. *out_len = bak_out_len;
  916. *in_len = bak_in_len;
  917. if (in != NULL)
  918. speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len);
  919. else
  920. speex_resampler_process_float(st, i, NULL, in_len, out+i, out_len);
  921. }
  922. st->in_stride = istride_save;
  923. st->out_stride = ostride_save;
  924. return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
  925. }
  926. 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)
  927. {
  928. spx_uint32_t i;
  929. int istride_save, ostride_save;
  930. spx_uint32_t bak_out_len = *out_len;
  931. spx_uint32_t bak_in_len = *in_len;
  932. istride_save = st->in_stride;
  933. ostride_save = st->out_stride;
  934. st->in_stride = st->out_stride = st->nb_channels;
  935. for (i=0;i<st->nb_channels;i++)
  936. {
  937. *out_len = bak_out_len;
  938. *in_len = bak_in_len;
  939. if (in != NULL)
  940. speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len);
  941. else
  942. speex_resampler_process_int(st, i, NULL, in_len, out+i, out_len);
  943. }
  944. st->in_stride = istride_save;
  945. st->out_stride = ostride_save;
  946. return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
  947. }
  948. EXPORT int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
  949. {
  950. return speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate);
  951. }
  952. EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate)
  953. {
  954. *in_rate = st->in_rate;
  955. *out_rate = st->out_rate;
  956. }
  957. 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)
  958. {
  959. spx_uint32_t fact;
  960. spx_uint32_t old_den;
  961. spx_uint32_t i;
  962. if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den)
  963. return RESAMPLER_ERR_SUCCESS;
  964. old_den = st->den_rate;
  965. st->in_rate = in_rate;
  966. st->out_rate = out_rate;
  967. st->num_rate = ratio_num;
  968. st->den_rate = ratio_den;
  969. /* FIXME: This is terribly inefficient, but who cares (at least for now)? */
  970. for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++)
  971. {
  972. while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0))
  973. {
  974. st->num_rate /= fact;
  975. st->den_rate /= fact;
  976. }
  977. }
  978. if (old_den > 0)
  979. {
  980. for (i=0;i<st->nb_channels;i++)
  981. {
  982. st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den;
  983. /* Safety net */
  984. if (st->samp_frac_num[i] >= st->den_rate)
  985. st->samp_frac_num[i] = st->den_rate-1;
  986. }
  987. }
  988. if (st->initialised)
  989. return update_filter(st);
  990. return RESAMPLER_ERR_SUCCESS;
  991. }
  992. EXPORT void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den)
  993. {
  994. *ratio_num = st->num_rate;
  995. *ratio_den = st->den_rate;
  996. }
  997. EXPORT int speex_resampler_set_quality(SpeexResamplerState *st, int quality)
  998. {
  999. if (quality > 10 || quality < 0)
  1000. return RESAMPLER_ERR_INVALID_ARG;
  1001. if (st->quality == quality)
  1002. return RESAMPLER_ERR_SUCCESS;
  1003. st->quality = quality;
  1004. if (st->initialised)
  1005. return update_filter(st);
  1006. return RESAMPLER_ERR_SUCCESS;
  1007. }
  1008. EXPORT void speex_resampler_get_quality(SpeexResamplerState *st, int *quality)
  1009. {
  1010. *quality = st->quality;
  1011. }
  1012. EXPORT void speex_resampler_set_input_stride(SpeexResamplerState *st, spx_uint32_t stride)
  1013. {
  1014. st->in_stride = stride;
  1015. }
  1016. EXPORT void speex_resampler_get_input_stride(SpeexResamplerState *st, spx_uint32_t *stride)
  1017. {
  1018. *stride = st->in_stride;
  1019. }
  1020. EXPORT void speex_resampler_set_output_stride(SpeexResamplerState *st, spx_uint32_t stride)
  1021. {
  1022. st->out_stride = stride;
  1023. }
  1024. EXPORT void speex_resampler_get_output_stride(SpeexResamplerState *st, spx_uint32_t *stride)
  1025. {
  1026. *stride = st->out_stride;
  1027. }
  1028. EXPORT int speex_resampler_get_input_latency(SpeexResamplerState *st)
  1029. {
  1030. return st->filt_len / 2;
  1031. }
  1032. EXPORT int speex_resampler_get_output_latency(SpeexResamplerState *st)
  1033. {
  1034. return ((st->filt_len / 2) * st->den_rate + (st->num_rate >> 1)) / st->num_rate;
  1035. }
  1036. EXPORT int speex_resampler_skip_zeros(SpeexResamplerState *st)
  1037. {
  1038. spx_uint32_t i;
  1039. for (i=0;i<st->nb_channels;i++)
  1040. st->last_sample[i] = st->filt_len/2;
  1041. return RESAMPLER_ERR_SUCCESS;
  1042. }
  1043. EXPORT int speex_resampler_reset_mem(SpeexResamplerState *st)
  1044. {
  1045. spx_uint32_t i;
  1046. for (i=0;i<st->nb_channels;i++)
  1047. {
  1048. st->last_sample[i] = 0;
  1049. st->magic_samples[i] = 0;
  1050. st->samp_frac_num[i] = 0;
  1051. }
  1052. for (i=0;i<st->nb_channels*(st->filt_len-1);i++)
  1053. st->mem[i] = 0;
  1054. return RESAMPLER_ERR_SUCCESS;
  1055. }
  1056. EXPORT const char *speex_resampler_strerror(int err)
  1057. {
  1058. switch (err)
  1059. {
  1060. case RESAMPLER_ERR_SUCCESS:
  1061. return "Success.";
  1062. case RESAMPLER_ERR_ALLOC_FAILED:
  1063. return "Memory allocation failed.";
  1064. case RESAMPLER_ERR_BAD_STATE:
  1065. return "Bad resampler state.";
  1066. case RESAMPLER_ERR_INVALID_ARG:
  1067. return "Invalid argument.";
  1068. case RESAMPLER_ERR_PTR_OVERLAP:
  1069. return "Input and output buffers overlap.";
  1070. default:
  1071. return "Unknown error. Bad error code or strange version mismatch.";
  1072. }
  1073. }