SDL_audioresample.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_sysaudio.h"
  20. #include "SDL_audioresample.h"
  21. // SDL's resampler uses a "bandlimited interpolation" algorithm:
  22. // https://ccrma.stanford.edu/~jos/resample/
  23. // TODO: Support changing this at runtime?
  24. #if defined(SDL_SSE_INTRINSICS) || defined(SDL_NEON_INTRINSICS)
  25. // In <current year>, SSE is basically mandatory anyway
  26. // We want RESAMPLER_SAMPLES_PER_FRAME to be a multiple of 4, to make SIMD easier
  27. #define RESAMPLER_ZERO_CROSSINGS 6
  28. #else
  29. #define RESAMPLER_ZERO_CROSSINGS 5
  30. #endif
  31. #define RESAMPLER_SAMPLES_PER_FRAME (RESAMPLER_ZERO_CROSSINGS * 2)
  32. // For a given srcpos, `srcpos + frame` are sampled, where `-RESAMPLER_ZERO_CROSSINGS < frame <= RESAMPLER_ZERO_CROSSINGS`.
  33. // Note, when upsampling, it is also possible to start sampling from `srcpos = -1`.
  34. #define RESAMPLER_MAX_PADDING_FRAMES (RESAMPLER_ZERO_CROSSINGS + 1)
  35. // More bits gives more precision, at the cost of a larger table.
  36. #define RESAMPLER_BITS_PER_ZERO_CROSSING 3
  37. #define RESAMPLER_SAMPLES_PER_ZERO_CROSSING (1 << RESAMPLER_BITS_PER_ZERO_CROSSING)
  38. #define RESAMPLER_FILTER_INTERP_BITS (32 - RESAMPLER_BITS_PER_ZERO_CROSSING)
  39. #define RESAMPLER_FILTER_INTERP_RANGE (1 << RESAMPLER_FILTER_INTERP_BITS)
  40. // ResampleFrame is just a vector/matrix/matrix multiplication.
  41. // It performs cubic interpolation of the filter, then multiplies that with the input.
  42. // dst = [1, frac, frac^2, frac^3] * filter * src
  43. // Cubic Polynomial
  44. typedef union Cubic
  45. {
  46. float v[4];
  47. #ifdef SDL_SSE_INTRINSICS
  48. // Aligned loads can be used directly as memory operands for mul/add
  49. __m128 v128;
  50. #endif
  51. #ifdef SDL_NEON_INTRINSICS
  52. float32x4_t v128;
  53. #endif
  54. } Cubic;
  55. static void ResampleFrame_Generic(const float *src, float *dst, const Cubic *filter, float frac, int chans)
  56. {
  57. const float frac2 = frac * frac;
  58. const float frac3 = frac * frac2;
  59. int i, chan;
  60. float scales[RESAMPLER_SAMPLES_PER_FRAME];
  61. for (i = 0; i < RESAMPLER_SAMPLES_PER_FRAME; ++i, ++filter) {
  62. scales[i] = filter->v[0] + (filter->v[1] * frac) + (filter->v[2] * frac2) + (filter->v[3] * frac3);
  63. }
  64. for (chan = 0; chan < chans; ++chan) {
  65. float out = 0.0f;
  66. for (i = 0; i < RESAMPLER_SAMPLES_PER_FRAME; ++i) {
  67. out += src[i * chans + chan] * scales[i];
  68. }
  69. dst[chan] = out;
  70. }
  71. }
  72. static void ResampleFrame_Mono(const float *src, float *dst, const Cubic *filter, float frac, int chans)
  73. {
  74. const float frac2 = frac * frac;
  75. const float frac3 = frac * frac2;
  76. int i;
  77. float out = 0.0f;
  78. for (i = 0; i < RESAMPLER_SAMPLES_PER_FRAME; ++i, ++filter) {
  79. // Interpolate between the nearest two filters
  80. const float scale = filter->v[0] + (filter->v[1] * frac) + (filter->v[2] * frac2) + (filter->v[3] * frac3);
  81. out += src[i] * scale;
  82. }
  83. dst[0] = out;
  84. }
  85. static void ResampleFrame_Stereo(const float *src, float *dst, const Cubic *filter, float frac, int chans)
  86. {
  87. const float frac2 = frac * frac;
  88. const float frac3 = frac * frac2;
  89. int i;
  90. float out0 = 0.0f;
  91. float out1 = 0.0f;
  92. for (i = 0; i < RESAMPLER_SAMPLES_PER_FRAME; ++i, ++filter) {
  93. // Interpolate between the nearest two filters
  94. const float scale = filter->v[0] + (filter->v[1] * frac) + (filter->v[2] * frac2) + (filter->v[3] * frac3);
  95. out0 += src[i * 2 + 0] * scale;
  96. out1 += src[i * 2 + 1] * scale;
  97. }
  98. dst[0] = out0;
  99. dst[1] = out1;
  100. }
  101. #ifdef SDL_SSE_INTRINSICS
  102. #define sdl_madd_ps(a, b, c) _mm_add_ps(a, _mm_mul_ps(b, c)) // Not-so-fused multiply-add
  103. static void SDL_TARGETING("sse") ResampleFrame_Generic_SSE(const float *src, float *dst, const Cubic *filter, float frac, int chans)
  104. {
  105. #if RESAMPLER_SAMPLES_PER_FRAME != 12
  106. #error Invalid samples per frame
  107. #endif
  108. __m128 f0, f1, f2;
  109. {
  110. const __m128 frac1 = _mm_set1_ps(frac);
  111. const __m128 frac2 = _mm_mul_ps(frac1, frac1);
  112. const __m128 frac3 = _mm_mul_ps(frac1, frac2);
  113. // Transposed in SetupAudioResampler
  114. // Explicitly use _mm_load_ps to workaround ICE in GCC 4.9.4 accessing Cubic.v128
  115. #define X(out) \
  116. out = _mm_load_ps(filter[0].v); \
  117. out = sdl_madd_ps(out, frac1, _mm_load_ps(filter[1].v)); \
  118. out = sdl_madd_ps(out, frac2, _mm_load_ps(filter[2].v)); \
  119. out = sdl_madd_ps(out, frac3, _mm_load_ps(filter[3].v)); \
  120. filter += 4
  121. X(f0);
  122. X(f1);
  123. X(f2);
  124. #undef X
  125. }
  126. if (chans == 2) {
  127. // Duplicate each of the filter elements and multiply by the input
  128. // Use two accumulators to improve throughput
  129. __m128 out0 = _mm_mul_ps(_mm_loadu_ps(src + 0), _mm_unpacklo_ps(f0, f0));
  130. __m128 out1 = _mm_mul_ps(_mm_loadu_ps(src + 4), _mm_unpackhi_ps(f0, f0));
  131. out0 = sdl_madd_ps(out0, _mm_loadu_ps(src + 8), _mm_unpacklo_ps(f1, f1));
  132. out1 = sdl_madd_ps(out1, _mm_loadu_ps(src + 12), _mm_unpackhi_ps(f1, f1));
  133. out0 = sdl_madd_ps(out0, _mm_loadu_ps(src + 16), _mm_unpacklo_ps(f2, f2));
  134. out1 = sdl_madd_ps(out1, _mm_loadu_ps(src + 20), _mm_unpackhi_ps(f2, f2));
  135. // Add the accumulators together
  136. __m128 out = _mm_add_ps(out0, out1);
  137. // Add the lower and upper pairs together
  138. out = _mm_add_ps(out, _mm_movehl_ps(out, out));
  139. // Store the result
  140. _mm_storel_pi((__m64 *)dst, out);
  141. return;
  142. }
  143. if (chans == 1) {
  144. // Multiply the filter by the input
  145. __m128 out = _mm_mul_ps(f0, _mm_loadu_ps(src + 0));
  146. out = sdl_madd_ps(out, f1, _mm_loadu_ps(src + 4));
  147. out = sdl_madd_ps(out, f2, _mm_loadu_ps(src + 8));
  148. // Horizontal sum
  149. __m128 shuf = _mm_shuffle_ps(out, out, _MM_SHUFFLE(2, 3, 0, 1));
  150. out = _mm_add_ps(out, shuf);
  151. out = _mm_add_ss(out, _mm_movehl_ps(shuf, out));
  152. _mm_store_ss(dst, out);
  153. return;
  154. }
  155. int chan = 0;
  156. // Process 4 channels at once
  157. for (; chan + 4 <= chans; chan += 4) {
  158. const float *in = &src[chan];
  159. __m128 out0 = _mm_setzero_ps();
  160. __m128 out1 = _mm_setzero_ps();
  161. #define X(a, b, out) \
  162. out = sdl_madd_ps(out, _mm_loadu_ps(in), _mm_shuffle_ps(a, a, _MM_SHUFFLE(b, b, b, b))); \
  163. in += chans
  164. #define Y(a) \
  165. X(a, 0, out0); \
  166. X(a, 1, out1); \
  167. X(a, 2, out0); \
  168. X(a, 3, out1)
  169. Y(f0);
  170. Y(f1);
  171. Y(f2);
  172. #undef X
  173. #undef Y
  174. // Add the accumulators together
  175. __m128 out = _mm_add_ps(out0, out1);
  176. _mm_storeu_ps(&dst[chan], out);
  177. }
  178. // Process the remaining channels one at a time.
  179. // Channel counts 1,2,4,8 are already handled above, leaving 3,5,6,7 to deal with (looping 3,1,2,3 times).
  180. // Without vgatherdps (AVX2), this gets quite messy.
  181. for (; chan < chans; ++chan) {
  182. const float *in = &src[chan];
  183. __m128 v0, v1, v2;
  184. #define X(x) \
  185. x = _mm_unpacklo_ps(_mm_load_ss(in), _mm_load_ss(in + chans)); \
  186. in += chans + chans; \
  187. x = _mm_movelh_ps(x, _mm_unpacklo_ps(_mm_load_ss(in), _mm_load_ss(in + chans))); \
  188. in += chans + chans
  189. X(v0);
  190. X(v1);
  191. X(v2);
  192. #undef X
  193. __m128 out = _mm_mul_ps(f0, v0);
  194. out = sdl_madd_ps(out, f1, v1);
  195. out = sdl_madd_ps(out, f2, v2);
  196. // Horizontal sum
  197. __m128 shuf = _mm_shuffle_ps(out, out, _MM_SHUFFLE(2, 3, 0, 1));
  198. out = _mm_add_ps(out, shuf);
  199. out = _mm_add_ss(out, _mm_movehl_ps(shuf, out));
  200. _mm_store_ss(&dst[chan], out);
  201. }
  202. }
  203. #undef sdl_madd_ps
  204. #endif
  205. #ifdef SDL_NEON_INTRINSICS
  206. static void ResampleFrame_Generic_NEON(const float *src, float *dst, const Cubic *filter, float frac, int chans)
  207. {
  208. #if RESAMPLER_SAMPLES_PER_FRAME != 12
  209. #error Invalid samples per frame
  210. #endif
  211. float32x4_t f0, f1, f2;
  212. {
  213. const float32x4_t frac1 = vdupq_n_f32(frac);
  214. const float32x4_t frac2 = vmulq_f32(frac1, frac1);
  215. const float32x4_t frac3 = vmulq_f32(frac1, frac2);
  216. // Transposed in SetupAudioResampler
  217. #define X(out) \
  218. out = vmlaq_f32(vmlaq_f32(vmlaq_f32(filter[0].v128, filter[1].v128, frac1), filter[2].v128, frac2), filter[3].v128, frac3); \
  219. filter += 4
  220. X(f0);
  221. X(f1);
  222. X(f2);
  223. #undef X
  224. }
  225. if (chans == 2) {
  226. float32x4x2_t g0 = vzipq_f32(f0, f0);
  227. float32x4x2_t g1 = vzipq_f32(f1, f1);
  228. float32x4x2_t g2 = vzipq_f32(f2, f2);
  229. // Duplicate each of the filter elements and multiply by the input
  230. // Use two accumulators to improve throughput
  231. float32x4_t out0 = vmulq_f32(vld1q_f32(src + 0), g0.val[0]);
  232. float32x4_t out1 = vmulq_f32(vld1q_f32(src + 4), g0.val[1]);
  233. out0 = vmlaq_f32(out0, vld1q_f32(src + 8), g1.val[0]);
  234. out1 = vmlaq_f32(out1, vld1q_f32(src + 12), g1.val[1]);
  235. out0 = vmlaq_f32(out0, vld1q_f32(src + 16), g2.val[0]);
  236. out1 = vmlaq_f32(out1, vld1q_f32(src + 20), g2.val[1]);
  237. // Add the accumulators together
  238. out0 = vaddq_f32(out0, out1);
  239. // Add the lower and upper pairs together
  240. float32x2_t out = vadd_f32(vget_low_f32(out0), vget_high_f32(out0));
  241. // Store the result
  242. vst1_f32(dst, out);
  243. return;
  244. }
  245. if (chans == 1) {
  246. // Multiply the filter by the input
  247. float32x4_t out = vmulq_f32(f0, vld1q_f32(src + 0));
  248. out = vmlaq_f32(out, f1, vld1q_f32(src + 4));
  249. out = vmlaq_f32(out, f2, vld1q_f32(src + 8));
  250. // Horizontal sum
  251. float32x2_t sum = vadd_f32(vget_low_f32(out), vget_high_f32(out));
  252. sum = vpadd_f32(sum, sum);
  253. vst1_lane_f32(dst, sum, 0);
  254. return;
  255. }
  256. int chan = 0;
  257. // Process 4 channels at once
  258. for (; chan + 4 <= chans; chan += 4) {
  259. const float *in = &src[chan];
  260. float32x4_t out0 = vdupq_n_f32(0);
  261. float32x4_t out1 = vdupq_n_f32(0);
  262. #define X(a, b, out) \
  263. out = vmlaq_f32(out, vld1q_f32(in), vdupq_lane_f32(a, b)); \
  264. in += chans
  265. #define Y(a) \
  266. X(vget_low_f32(a), 0, out0); \
  267. X(vget_low_f32(a), 1, out1); \
  268. X(vget_high_f32(a), 0, out0); \
  269. X(vget_high_f32(a), 1, out1)
  270. Y(f0);
  271. Y(f1);
  272. Y(f2);
  273. #undef X
  274. #undef Y
  275. // Add the accumulators together
  276. float32x4_t out = vaddq_f32(out0, out1);
  277. vst1q_f32(&dst[chan], out);
  278. }
  279. // Process the remaining channels one at a time.
  280. // Channel counts 1,2,4,8 are already handled above, leaving 3,5,6,7 to deal with (looping 3,1,2,3 times).
  281. for (; chan < chans; ++chan) {
  282. const float *in = &src[chan];
  283. float32x4_t v0, v1, v2;
  284. #define X(x) \
  285. x = vld1q_dup_f32(in); \
  286. in += chans; \
  287. x = vld1q_lane_f32(in, x, 1); \
  288. in += chans; \
  289. x = vld1q_lane_f32(in, x, 2); \
  290. in += chans; \
  291. x = vld1q_lane_f32(in, x, 3); \
  292. in += chans
  293. X(v0);
  294. X(v1);
  295. X(v2);
  296. #undef X
  297. float32x4_t out = vmulq_f32(f0, v0);
  298. out = vmlaq_f32(out, f1, v1);
  299. out = vmlaq_f32(out, f2, v2);
  300. // Horizontal sum
  301. float32x2_t sum = vadd_f32(vget_low_f32(out), vget_high_f32(out));
  302. sum = vpadd_f32(sum, sum);
  303. vst1_lane_f32(&dst[chan], sum, 0);
  304. }
  305. }
  306. #endif
  307. // Calculate the cubic equation which passes through all four points.
  308. // https://en.wikipedia.org/wiki/Ordinary_least_squares
  309. // https://en.wikipedia.org/wiki/Polynomial_regression
  310. static void CubicLeastSquares(Cubic *coeffs, float y0, float y1, float y2, float y3)
  311. {
  312. // Least squares matrix for xs = [0, 1/3, 2/3, 1]
  313. // [ 1.0 0.0 0.0 0.0 ]
  314. // [ -5.5 9.0 -4.5 1.0 ]
  315. // [ 9.0 -22.5 18.0 -4.5 ]
  316. // [ -4.5 13.5 -13.5 4.5 ]
  317. coeffs->v[0] = y0;
  318. coeffs->v[1] = -5.5f * y0 + 9.0f * y1 - 4.5f * y2 + y3;
  319. coeffs->v[2] = 9.0f * y0 - 22.5f * y1 + 18.0f * y2 - 4.5f * y3;
  320. coeffs->v[3] = -4.5f * y0 + 13.5f * y1 - 13.5f * y2 + 4.5f * y3;
  321. }
  322. // Zeroth-order modified Bessel function of the first kind
  323. // https://mathworld.wolfram.com/ModifiedBesselFunctionoftheFirstKind.html
  324. static float BesselI0(float x)
  325. {
  326. float sum = 0.0f;
  327. float i = 1.0f;
  328. float t = 1.0f;
  329. x *= x * 0.25f;
  330. while (t >= sum * SDL_FLT_EPSILON) {
  331. sum += t;
  332. t *= x / (i * i);
  333. ++i;
  334. }
  335. return sum;
  336. }
  337. // Pre-calculate 180 degrees of sin(pi * x) / pi
  338. // The speedup from this isn't huge, but it also avoids precision issues.
  339. // If sinf isn't available, SDL_sinf just calls SDL_sin.
  340. // Know what SDL_sin(SDL_PI_F) equals? Not quite zero.
  341. static void SincTable(float *table, int len)
  342. {
  343. int i;
  344. for (i = 0; i < len; ++i) {
  345. table[i] = SDL_sinf(i * (SDL_PI_F / len)) / SDL_PI_F;
  346. }
  347. }
  348. // Calculate Sinc(x/y), using a lookup table
  349. static float Sinc(float *table, int x, int y)
  350. {
  351. float s = table[x % y];
  352. s = ((x / y) & 1) ? -s : s;
  353. return (s * y) / x;
  354. }
  355. static Cubic ResamplerFilter[RESAMPLER_SAMPLES_PER_ZERO_CROSSING][RESAMPLER_SAMPLES_PER_FRAME];
  356. static void GenerateResamplerFilter(void)
  357. {
  358. enum
  359. {
  360. // Generate samples at 3x the target resolution, so that we have samples at [0, 1/3, 2/3, 1] of each position
  361. TABLE_SAMPLES_PER_ZERO_CROSSING = RESAMPLER_SAMPLES_PER_ZERO_CROSSING * 3,
  362. TABLE_SIZE = RESAMPLER_ZERO_CROSSINGS * TABLE_SAMPLES_PER_ZERO_CROSSING,
  363. };
  364. // if dB > 50, beta=(0.1102 * (dB - 8.7)), according to Matlab.
  365. const float dB = 80.0f;
  366. const float beta = 0.1102f * (dB - 8.7f);
  367. const float bessel_beta = BesselI0(beta);
  368. const float lensqr = TABLE_SIZE * TABLE_SIZE;
  369. int i, j;
  370. float sinc[TABLE_SAMPLES_PER_ZERO_CROSSING];
  371. SincTable(sinc, TABLE_SAMPLES_PER_ZERO_CROSSING);
  372. // Generate one wing of the filter
  373. // https://en.wikipedia.org/wiki/Kaiser_window
  374. // https://en.wikipedia.org/wiki/Whittaker%E2%80%93Shannon_interpolation_formula
  375. float filter[TABLE_SIZE + 1];
  376. filter[0] = 1.0f;
  377. for (i = 1; i <= TABLE_SIZE; ++i) {
  378. float b = BesselI0(beta * SDL_sqrtf((lensqr - (i * i)) / lensqr)) / bessel_beta;
  379. float s = Sinc(sinc, i, TABLE_SAMPLES_PER_ZERO_CROSSING);
  380. filter[i] = b * s;
  381. }
  382. // Generate the coefficients for each point
  383. // When interpolating, the fraction represents how far we are between input samples,
  384. // so we need to align the filter by "moving" it to the right.
  385. //
  386. // For the left wing, this means interpolating "forwards" (away from the center)
  387. // For the right wing, this means interpolating "backwards" (towards the center)
  388. //
  389. // The center of the filter is at the end of the left wing (RESAMPLER_ZERO_CROSSINGS - 1)
  390. // The left wing is the filter, but reversed
  391. // The right wing is the filter, but offset by 1
  392. //
  393. // Since the right wing is offset by 1, this just means we interpolate backwards
  394. // between the same points, instead of forwards
  395. // interp(p[n], p[n+1], t) = interp(p[n+1], p[n+1-1], 1 - t) = interp(p[n+1], p[n], 1 - t)
  396. for (i = 0; i < RESAMPLER_SAMPLES_PER_ZERO_CROSSING; ++i) {
  397. for (j = 0; j < RESAMPLER_ZERO_CROSSINGS; ++j) {
  398. const float *ys = &filter[((j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING) + i) * 3];
  399. Cubic *fwd = &ResamplerFilter[i][RESAMPLER_ZERO_CROSSINGS - j - 1];
  400. Cubic *rev = &ResamplerFilter[RESAMPLER_SAMPLES_PER_ZERO_CROSSING - i - 1][RESAMPLER_ZERO_CROSSINGS + j];
  401. // Calculate the cubic equation of the 4 points
  402. CubicLeastSquares(fwd, ys[0], ys[1], ys[2], ys[3]);
  403. CubicLeastSquares(rev, ys[3], ys[2], ys[1], ys[0]);
  404. }
  405. }
  406. }
  407. typedef void (*ResampleFrameFunc)(const float *src, float *dst, const Cubic *filter, float frac, int chans);
  408. static ResampleFrameFunc ResampleFrame[8];
  409. // Transpose 4x4 floats
  410. static void Transpose4x4(Cubic *data)
  411. {
  412. int i, j;
  413. Cubic temp[4] = { data[0], data[1], data[2], data[3] };
  414. for (i = 0; i < 4; ++i) {
  415. for (j = 0; j < 4; ++j) {
  416. data[i].v[j] = temp[j].v[i];
  417. }
  418. }
  419. }
  420. static void SetupAudioResampler(void)
  421. {
  422. int i, j;
  423. SDL_bool transpose = SDL_FALSE;
  424. GenerateResamplerFilter();
  425. #ifdef SDL_SSE_INTRINSICS
  426. if (SDL_HasSSE()) {
  427. for (i = 0; i < 8; ++i) {
  428. ResampleFrame[i] = ResampleFrame_Generic_SSE;
  429. }
  430. transpose = SDL_TRUE;
  431. } else
  432. #endif
  433. #ifdef SDL_NEON_INTRINSICS
  434. if (SDL_HasNEON()) {
  435. for (i = 0; i < 8; ++i) {
  436. ResampleFrame[i] = ResampleFrame_Generic_NEON;
  437. }
  438. transpose = SDL_TRUE;
  439. } else
  440. #endif
  441. {
  442. for (i = 0; i < 8; ++i) {
  443. ResampleFrame[i] = ResampleFrame_Generic;
  444. }
  445. ResampleFrame[0] = ResampleFrame_Mono;
  446. ResampleFrame[1] = ResampleFrame_Stereo;
  447. }
  448. if (transpose) {
  449. // Transpose each set of 4 coefficients, to reduce work when resampling
  450. for (i = 0; i < RESAMPLER_SAMPLES_PER_ZERO_CROSSING; ++i) {
  451. for (j = 0; j + 4 <= RESAMPLER_SAMPLES_PER_FRAME; j += 4) {
  452. Transpose4x4(&ResamplerFilter[i][j]);
  453. }
  454. }
  455. }
  456. }
  457. void SDL_SetupAudioResampler(void)
  458. {
  459. static SDL_SpinLock running = 0;
  460. if (!ResampleFrame[0]) {
  461. SDL_LockSpinlock(&running);
  462. if (!ResampleFrame[0]) {
  463. SetupAudioResampler();
  464. }
  465. SDL_UnlockSpinlock(&running);
  466. }
  467. }
  468. Sint64 SDL_GetResampleRate(int src_rate, int dst_rate)
  469. {
  470. SDL_assert(src_rate > 0);
  471. SDL_assert(dst_rate > 0);
  472. Sint64 sample_rate = ((Sint64)src_rate << 32) / (Sint64)dst_rate;
  473. SDL_assert(sample_rate > 0);
  474. return sample_rate;
  475. }
  476. int SDL_GetResamplerHistoryFrames(void)
  477. {
  478. // Even if we aren't currently resampling, make sure to keep enough history in case we need to later.
  479. return RESAMPLER_MAX_PADDING_FRAMES;
  480. }
  481. int SDL_GetResamplerPaddingFrames(Sint64 resample_rate)
  482. {
  483. // This must always be <= SDL_GetResamplerHistoryFrames()
  484. return resample_rate ? RESAMPLER_MAX_PADDING_FRAMES : 0;
  485. }
  486. // These are not general purpose. They do not check for all possible underflow/overflow
  487. SDL_FORCE_INLINE Sint64 ResamplerAdd(Sint64 a, Sint64 b, Sint64 *ret)
  488. {
  489. if ((b > 0) && (a > SDL_MAX_SINT64 - b)) {
  490. return -1;
  491. }
  492. *ret = a + b;
  493. return 0;
  494. }
  495. SDL_FORCE_INLINE Sint64 ResamplerMul(Sint64 a, Sint64 b, Sint64 *ret)
  496. {
  497. if ((b > 0) && (a > SDL_MAX_SINT64 / b)) {
  498. return -1;
  499. }
  500. *ret = a * b;
  501. return 0;
  502. }
  503. Sint64 SDL_GetResamplerInputFrames(Sint64 output_frames, Sint64 resample_rate, Sint64 resample_offset)
  504. {
  505. // Calculate the index of the last input frame, then add 1.
  506. // ((((output_frames - 1) * resample_rate) + resample_offset) >> 32) + 1
  507. Sint64 output_offset;
  508. if (ResamplerMul(output_frames, resample_rate, &output_offset) ||
  509. ResamplerAdd(output_offset, -resample_rate + resample_offset + 0x100000000, &output_offset)) {
  510. output_offset = SDL_MAX_SINT64;
  511. }
  512. Sint64 input_frames = (Sint64)(Sint32)(output_offset >> 32);
  513. input_frames = SDL_max(input_frames, 0);
  514. return input_frames;
  515. }
  516. Sint64 SDL_GetResamplerOutputFrames(Sint64 input_frames, Sint64 resample_rate, Sint64 *inout_resample_offset)
  517. {
  518. Sint64 resample_offset = *inout_resample_offset;
  519. // input_offset = (input_frames << 32) - resample_offset;
  520. Sint64 input_offset;
  521. if (ResamplerMul(input_frames, 0x100000000, &input_offset) ||
  522. ResamplerAdd(input_offset, -resample_offset, &input_offset)) {
  523. input_offset = SDL_MAX_SINT64;
  524. }
  525. // output_frames = div_ceil(input_offset, resample_rate)
  526. Sint64 output_frames = (input_offset > 0) ? (((input_offset - 1) / resample_rate) + 1) : 0;
  527. *inout_resample_offset = (output_frames * resample_rate) - input_offset;
  528. return output_frames;
  529. }
  530. void SDL_ResampleAudio(int chans, const float *src, int inframes, float *dst, int outframes,
  531. Sint64 resample_rate, Sint64 *inout_resample_offset)
  532. {
  533. int i;
  534. Sint64 srcpos = *inout_resample_offset;
  535. ResampleFrameFunc resample_frame = ResampleFrame[chans - 1];
  536. SDL_assert(resample_rate > 0);
  537. src -= (RESAMPLER_ZERO_CROSSINGS - 1) * chans;
  538. for (i = 0; i < outframes; ++i) {
  539. int srcindex = (int)(Sint32)(srcpos >> 32);
  540. Uint32 srcfraction = (Uint32)(srcpos & 0xFFFFFFFF);
  541. srcpos += resample_rate;
  542. SDL_assert(srcindex >= -1 && srcindex < inframes);
  543. const Cubic *filter = ResamplerFilter[srcfraction >> RESAMPLER_FILTER_INTERP_BITS];
  544. const float frac = (float)(srcfraction & (RESAMPLER_FILTER_INTERP_RANGE - 1)) * (1.0f / RESAMPLER_FILTER_INTERP_RANGE);
  545. const float *frame = &src[srcindex * chans];
  546. resample_frame(frame, dst, filter, frac, chans);
  547. dst += chans;
  548. }
  549. *inout_resample_offset = srcpos - ((Sint64)inframes << 32);
  550. }