resample.c 39 KB

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