jack2 codebase
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.

1470 lines
41KB

  1. /*
  2. Copyright (C) 2000 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #define _ISOC9X_SOURCE 1
  16. #define _ISOC99_SOURCE 1
  17. #define __USE_ISOC9X 1
  18. #define __USE_ISOC99 1
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <memory.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <limits.h>
  26. #ifdef __linux__
  27. #include <endian.h>
  28. #endif
  29. #include "memops.h"
  30. #if defined (__SSE2__) && !defined (__sun__)
  31. #include <emmintrin.h>
  32. #ifdef __SSE4_1__
  33. #include <smmintrin.h>
  34. #endif
  35. #endif
  36. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  37. #include <arm_neon.h>
  38. #endif
  39. /* Notes about these *_SCALING values.
  40. the MAX_<N>BIT values are floating point. when multiplied by
  41. a full-scale normalized floating point sample value (-1.0..+1.0)
  42. they should give the maximum value representable with an integer
  43. sample type of N bits. Note that this is asymmetric. Sample ranges
  44. for signed integer, 2's complement values are -(2^(N-1) to +(2^(N-1)-1)
  45. Complications
  46. -------------
  47. If we use +2^(N-1) for the scaling factors, we run into a problem:
  48. if we start with a normalized float value of -1.0, scaling
  49. to 24 bits would give -8388608 (-2^23), which is ideal.
  50. But with +1.0, we get +8388608, which is technically out of range.
  51. We never multiply a full range normalized value by this constant,
  52. but we could multiply it by a positive value that is close enough to +1.0
  53. to produce a value > +(2^(N-1)-1.
  54. There is no way around this paradox without wasting CPU cycles to determine
  55. which scaling factor to use (i.e. determine if its negative or not,
  56. use the right factor).
  57. So, for now (October 2008) we use 2^(N-1)-1 as the scaling factor.
  58. */
  59. #define SAMPLE_24BIT_SCALING 8388607.0f
  60. #define SAMPLE_16BIT_SCALING 32767.0f
  61. /* these are just values to use if the floating point value was out of range
  62. advice from Fons Adriaensen: make the limits symmetrical
  63. */
  64. #define SAMPLE_24BIT_MAX 8388607
  65. #define SAMPLE_24BIT_MIN -8388607
  66. #define SAMPLE_24BIT_MAX_F 8388607.0f
  67. #define SAMPLE_24BIT_MIN_F -8388607.0f
  68. #define SAMPLE_16BIT_MAX 32767
  69. #define SAMPLE_16BIT_MIN -32767
  70. #define SAMPLE_16BIT_MAX_F 32767.0f
  71. #define SAMPLE_16BIT_MIN_F -32767.0f
  72. /* these mark the outer edges of the range considered "within" range
  73. for a floating point sample value. values outside (and on the boundaries)
  74. of this range will be clipped before conversion; values within this
  75. range will be scaled to appropriate values for the target sample
  76. type.
  77. */
  78. #define NORMALIZED_FLOAT_MIN -1.0f
  79. #define NORMALIZED_FLOAT_MAX 1.0f
  80. /* define this in case we end up on a platform that is missing
  81. the real lrintf functions
  82. */
  83. #define f_round(f) lrintf(f)
  84. #define float_16(s, d)\
  85. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  86. (d) = SAMPLE_16BIT_MIN;\
  87. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  88. (d) = SAMPLE_16BIT_MAX;\
  89. } else {\
  90. (d) = f_round ((s) * SAMPLE_16BIT_SCALING);\
  91. }
  92. /* call this when "s" has already been scaled (e.g. when dithering)
  93. */
  94. #define float_16_scaled(s, d)\
  95. if ((s) <= SAMPLE_16BIT_MIN_F) {\
  96. (d) = SAMPLE_16BIT_MIN_F;\
  97. } else if ((s) >= SAMPLE_16BIT_MAX_F) { \
  98. (d) = SAMPLE_16BIT_MAX;\
  99. } else {\
  100. (d) = f_round ((s));\
  101. }
  102. #define float_24u32(s, d) \
  103. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  104. (d) = SAMPLE_24BIT_MIN << 8;\
  105. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  106. (d) = SAMPLE_24BIT_MAX << 8;\
  107. } else {\
  108. (d) = f_round ((s) * SAMPLE_24BIT_SCALING) << 8;\
  109. }
  110. #define float_24l32(s, d) \
  111. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  112. (d) = SAMPLE_24BIT_MIN; \
  113. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  114. (d) = SAMPLE_24BIT_MAX; \
  115. } else {\
  116. (d) = f_round ((s) * SAMPLE_24BIT_SCALING); \
  117. }
  118. /* call this when "s" has already been scaled (e.g. when dithering)
  119. */
  120. #define float_24u32_scaled(s, d)\
  121. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  122. (d) = SAMPLE_24BIT_MIN << 8;\
  123. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  124. (d) = SAMPLE_24BIT_MAX << 8; \
  125. } else {\
  126. (d) = f_round ((s)) << 8; \
  127. }
  128. #define float_24(s, d) \
  129. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  130. (d) = SAMPLE_24BIT_MIN;\
  131. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  132. (d) = SAMPLE_24BIT_MAX;\
  133. } else {\
  134. (d) = f_round ((s) * SAMPLE_24BIT_SCALING);\
  135. }
  136. /* call this when "s" has already been scaled (e.g. when dithering)
  137. */
  138. #define float_24_scaled(s, d)\
  139. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  140. (d) = SAMPLE_24BIT_MIN;\
  141. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  142. (d) = SAMPLE_24BIT_MAX; \
  143. } else {\
  144. (d) = f_round ((s)); \
  145. }
  146. #if defined (__SSE2__) && !defined (__sun__)
  147. /* generates same as _mm_set_ps(1.f, 1.f, 1f., 1f) but faster */
  148. static inline __m128 gen_one(void)
  149. {
  150. volatile __m128i x = { 0 }; /* shut up, GCC */
  151. __m128i ones = _mm_cmpeq_epi32(x, x);
  152. return (__m128)_mm_slli_epi32 (_mm_srli_epi32(ones, 25), 23);
  153. }
  154. static inline __m128 clip(__m128 s, __m128 min, __m128 max)
  155. {
  156. return _mm_min_ps(max, _mm_max_ps(s, min));
  157. }
  158. static inline __m128i float_24_sse(__m128 s)
  159. {
  160. const __m128 upper_bound = gen_one(); /* NORMALIZED_FLOAT_MAX */
  161. const __m128 lower_bound = _mm_sub_ps(_mm_setzero_ps(), upper_bound);
  162. __m128 clipped = clip(s, lower_bound, upper_bound);
  163. __m128 scaled = _mm_mul_ps(clipped, _mm_set1_ps(SAMPLE_24BIT_SCALING));
  164. return _mm_cvtps_epi32(scaled);
  165. }
  166. #endif
  167. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  168. static inline float32x4_t clip(float32x4_t s, float32x4_t min, float32x4_t max)
  169. {
  170. return vminq_f32(max, vmaxq_f32(s, min));
  171. }
  172. static inline int32x4_t float_24_neon(float32x4_t s)
  173. {
  174. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  175. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  176. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  177. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_24BIT_SCALING));
  178. return vcvtq_s32_f32(scaled);
  179. }
  180. static inline int16x4_t float_16_neon(float32x4_t s)
  181. {
  182. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  183. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  184. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  185. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_16BIT_SCALING));
  186. return vmovn_s32(vcvtq_s32_f32(scaled));
  187. }
  188. #endif
  189. /* Linear Congruential noise generator. From the music-dsp list
  190. * less random than rand(), but good enough and 10x faster
  191. */
  192. static unsigned int seed = 22222;
  193. static inline unsigned int fast_rand() {
  194. seed = (seed * 196314165) + 907633515;
  195. return seed;
  196. }
  197. /* functions for native float sample data */
  198. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  199. while (nsamples--) {
  200. *dst = *((float *) src);
  201. dst++;
  202. src += src_skip;
  203. }
  204. }
  205. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  206. while (nsamples--) {
  207. *((float *) dst) = *src;
  208. dst += dst_skip;
  209. src++;
  210. }
  211. }
  212. /* NOTES on function naming:
  213. foo_bar_d<TYPE>_s<TYPE>
  214. the "d<TYPE>" component defines the destination type for the operation
  215. the "s<TYPE>" component defines the source type for the operation
  216. TYPE can be one of:
  217. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  218. Ss - like S but reverse endian from the host CPU
  219. 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
  220. 32u24s - like 32u24 but reverse endian from the host CPU
  221. 32l24 - sample is an signed 32 bit integer value, but data is in lower 24 bits only
  222. 32l24s - like 32l24 but reverse endian from the host CPU
  223. 24 - sample is an signed 24 bit integer value
  224. 24s - like 24 but reverse endian from the host CPU
  225. 16 - sample is an signed 16 bit integer value
  226. 16s - like 16 but reverse endian from the host CPU
  227. For obvious reasons, the reverse endian versions only show as source types.
  228. This covers all known sample formats at 16 bits or larger.
  229. */
  230. /* functions for native integer sample data */
  231. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  232. {
  233. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  234. unsigned long unrolled = nsamples / 4;
  235. nsamples = nsamples & 3;
  236. while (unrolled--) {
  237. float32x4_t samples = vld1q_f32(src);
  238. int32x4_t converted = float_24_neon(samples);
  239. int32x4_t shifted = vshlq_n_s32(converted, 8);
  240. shifted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(shifted)));
  241. switch(dst_skip) {
  242. case 4:
  243. vst1q_s32((int32_t*)dst, shifted);
  244. break;
  245. default:
  246. vst1q_lane_s32((int32_t*)(dst), shifted, 0);
  247. vst1q_lane_s32((int32_t*)(dst+dst_skip), shifted, 1);
  248. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), shifted, 2);
  249. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), shifted, 3);
  250. break;
  251. }
  252. dst += 4*dst_skip;
  253. src+= 4;
  254. }
  255. #endif
  256. int32_t z;
  257. while (nsamples--) {
  258. float_24u32 (*src, z);
  259. #if __BYTE_ORDER == __LITTLE_ENDIAN
  260. dst[0]=(char)(z>>24);
  261. dst[1]=(char)(z>>16);
  262. dst[2]=(char)(z>>8);
  263. dst[3]=(char)(z);
  264. #elif __BYTE_ORDER == __BIG_ENDIAN
  265. dst[0]=(char)(z);
  266. dst[1]=(char)(z>>8);
  267. dst[2]=(char)(z>>16);
  268. dst[3]=(char)(z>>24);
  269. #endif
  270. dst += dst_skip;
  271. src++;
  272. }
  273. }
  274. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  275. {
  276. #if defined (__SSE2__) && !defined (__sun__)
  277. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  278. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  279. __m128 factor = int_max;
  280. unsigned long unrolled = nsamples / 4;
  281. nsamples = nsamples & 3;
  282. while (unrolled--) {
  283. __m128 in = _mm_load_ps(src);
  284. __m128 scaled = _mm_mul_ps(in, factor);
  285. __m128 clipped = clip(scaled, int_min, int_max);
  286. __m128i y = _mm_cvttps_epi32(clipped);
  287. __m128i shifted = _mm_slli_epi32(y, 8);
  288. #ifdef __SSE4_1__
  289. *(int32_t*)dst = _mm_extract_epi32(shifted, 0);
  290. *(int32_t*)(dst+dst_skip) = _mm_extract_epi32(shifted, 1);
  291. *(int32_t*)(dst+2*dst_skip) = _mm_extract_epi32(shifted, 2);
  292. *(int32_t*)(dst+3*dst_skip) = _mm_extract_epi32(shifted, 3);
  293. #else
  294. __m128i shuffled1 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(0, 3, 2, 1));
  295. __m128i shuffled2 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(1, 0, 3, 2));
  296. __m128i shuffled3 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(2, 1, 0, 3));
  297. _mm_store_ss((float*)dst, (__m128)shifted);
  298. _mm_store_ss((float*)(dst+dst_skip), (__m128)shuffled1);
  299. _mm_store_ss((float*)(dst+2*dst_skip), (__m128)shuffled2);
  300. _mm_store_ss((float*)(dst+3*dst_skip), (__m128)shuffled3);
  301. #endif
  302. dst += 4*dst_skip;
  303. src+= 4;
  304. }
  305. while (nsamples--) {
  306. __m128 in = _mm_load_ss(src);
  307. __m128 scaled = _mm_mul_ss(in, factor);
  308. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  309. int y = _mm_cvttss_si32(clipped);
  310. *((int *) dst) = y<<8;
  311. dst += dst_skip;
  312. src++;
  313. }
  314. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  315. unsigned long unrolled = nsamples / 4;
  316. nsamples = nsamples & 3;
  317. while (unrolled--) {
  318. float32x4_t samples = vld1q_f32(src);
  319. int32x4_t converted = float_24_neon(samples);
  320. int32x4_t shifted = vshlq_n_s32(converted, 8);
  321. switch(dst_skip) {
  322. case 4:
  323. vst1q_s32((int32_t*)dst, shifted);
  324. break;
  325. default:
  326. vst1q_lane_s32((int32_t*)(dst), shifted, 0);
  327. vst1q_lane_s32((int32_t*)(dst+dst_skip), shifted, 1);
  328. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), shifted, 2);
  329. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), shifted, 3);
  330. break;
  331. }
  332. dst += 4*dst_skip;
  333. src+= 4;
  334. }
  335. #endif
  336. #if !defined (__SSE2__)
  337. while (nsamples--) {
  338. float_24u32 (*src, *((int32_t*) dst));
  339. dst += dst_skip;
  340. src++;
  341. }
  342. #endif
  343. }
  344. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  345. {
  346. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  347. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  348. unsigned long unrolled = nsamples / 4;
  349. while (unrolled--) {
  350. int32x4_t src128;
  351. switch(src_skip)
  352. {
  353. case 4:
  354. src128 = vld1q_s32((int32_t*)src);
  355. break;
  356. case 8:
  357. src128 = vld2q_s32((int32_t*)src).val[0];
  358. break;
  359. default:
  360. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  361. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  362. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  363. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  364. break;
  365. }
  366. src128 = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(src128)));
  367. int32x4_t shifted = vshrq_n_s32(src128, 8);
  368. float32x4_t as_float = vcvtq_f32_s32(shifted);
  369. float32x4_t divided = vmulq_f32(as_float, factor);
  370. vst1q_f32(dst, divided);
  371. src += 4*src_skip;
  372. dst += 4;
  373. }
  374. nsamples = nsamples & 3;
  375. #endif
  376. /* ALERT: signed sign-extension portability !!! */
  377. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  378. while (nsamples--) {
  379. int x;
  380. #if __BYTE_ORDER == __LITTLE_ENDIAN
  381. x = (unsigned char)(src[0]);
  382. x <<= 8;
  383. x |= (unsigned char)(src[1]);
  384. x <<= 8;
  385. x |= (unsigned char)(src[2]);
  386. x <<= 8;
  387. x |= (unsigned char)(src[3]);
  388. #elif __BYTE_ORDER == __BIG_ENDIAN
  389. x = (unsigned char)(src[3]);
  390. x <<= 8;
  391. x |= (unsigned char)(src[2]);
  392. x <<= 8;
  393. x |= (unsigned char)(src[1]);
  394. x <<= 8;
  395. x |= (unsigned char)(src[0]);
  396. #endif
  397. *dst = (x >> 8) * scaling;
  398. dst++;
  399. src += src_skip;
  400. }
  401. }
  402. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  403. {
  404. #if defined (__SSE2__) && !defined (__sun__)
  405. unsigned long unrolled = nsamples / 4;
  406. static float inv_sample_max_24bit = 1.0 / SAMPLE_24BIT_SCALING;
  407. __m128 factor = _mm_set1_ps(inv_sample_max_24bit);
  408. while (unrolled--)
  409. {
  410. int i1 = *((int *) src);
  411. src+= src_skip;
  412. int i2 = *((int *) src);
  413. src+= src_skip;
  414. int i3 = *((int *) src);
  415. src+= src_skip;
  416. int i4 = *((int *) src);
  417. src+= src_skip;
  418. __m128i src = _mm_set_epi32(i4, i3, i2, i1);
  419. __m128i shifted = _mm_srai_epi32(src, 8);
  420. __m128 as_float = _mm_cvtepi32_ps(shifted);
  421. __m128 divided = _mm_mul_ps(as_float, factor);
  422. _mm_storeu_ps(dst, divided);
  423. dst += 4;
  424. }
  425. nsamples = nsamples & 3;
  426. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  427. unsigned long unrolled = nsamples / 4;
  428. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  429. while (unrolled--) {
  430. int32x4_t src128;
  431. switch(src_skip) {
  432. case 4:
  433. src128 = vld1q_s32((int32_t*)src);
  434. break;
  435. case 8:
  436. src128 = vld2q_s32((int32_t*)src).val[0];
  437. break;
  438. default:
  439. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  440. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  441. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  442. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  443. break;
  444. }
  445. int32x4_t shifted = vshrq_n_s32(src128, 8);
  446. float32x4_t as_float = vcvtq_f32_s32(shifted);
  447. float32x4_t divided = vmulq_f32(as_float, factor);
  448. vst1q_f32(dst, divided);
  449. src += 4*src_skip;
  450. dst += 4;
  451. }
  452. nsamples = nsamples & 3;
  453. #endif
  454. /* ALERT: signed sign-extension portability !!! */
  455. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  456. while (nsamples--) {
  457. *dst = (*((int *) src) >> 8) * scaling;
  458. dst++;
  459. src += src_skip;
  460. }
  461. }
  462. void sample_move_d32l24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  463. {
  464. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  465. unsigned long unrolled = nsamples / 4;
  466. nsamples = nsamples & 3;
  467. while (unrolled--) {
  468. float32x4_t samples = vld1q_f32(src);
  469. int32x4_t converted = float_24_neon(samples);
  470. converted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(converted)));
  471. switch(dst_skip) {
  472. case 4:
  473. vst1q_s32((int32_t*)dst, converted);
  474. break;
  475. default:
  476. vst1q_lane_s32((int32_t*)(dst), converted, 0);
  477. vst1q_lane_s32((int32_t*)(dst+dst_skip), converted, 1);
  478. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), converted, 2);
  479. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), converted, 3);
  480. break;
  481. }
  482. dst += 4*dst_skip;
  483. src+= 4;
  484. }
  485. #endif
  486. int32_t z;
  487. while (nsamples--) {
  488. float_24l32 (*src, z);
  489. #if __BYTE_ORDER == __LITTLE_ENDIAN
  490. dst[0]=(char)(z>>24);
  491. dst[1]=(char)(z>>16);
  492. dst[2]=(char)(z>>8);
  493. dst[3]=(char)(z);
  494. #elif __BYTE_ORDER == __BIG_ENDIAN
  495. dst[0]=(char)(z);
  496. dst[1]=(char)(z>>8);
  497. dst[2]=(char)(z>>16);
  498. dst[3]=(char)(z>>24);
  499. #endif
  500. dst += dst_skip;
  501. src++;
  502. }
  503. }
  504. void sample_move_d32l24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  505. {
  506. #if defined (__SSE2__) && !defined (__sun__)
  507. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  508. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  509. __m128 factor = int_max;
  510. unsigned long unrolled = nsamples / 4;
  511. nsamples = nsamples & 3;
  512. while (unrolled--) {
  513. __m128 in = _mm_load_ps(src);
  514. __m128 scaled = _mm_mul_ps(in, factor);
  515. __m128 clipped = clip(scaled, int_min, int_max);
  516. __m128i shifted = _mm_cvttps_epi32(clipped);
  517. #ifdef __SSE4_1__
  518. *(int32_t*)dst = _mm_extract_epi32(shifted, 0);
  519. *(int32_t*)(dst+dst_skip) = _mm_extract_epi32(shifted, 1);
  520. *(int32_t*)(dst+2*dst_skip) = _mm_extract_epi32(shifted, 2);
  521. *(int32_t*)(dst+3*dst_skip) = _mm_extract_epi32(shifted, 3);
  522. #else
  523. __m128i shuffled1 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(0, 3, 2, 1));
  524. __m128i shuffled2 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(1, 0, 3, 2));
  525. __m128i shuffled3 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(2, 1, 0, 3));
  526. _mm_store_ss((float*)dst, (__m128)shifted);
  527. _mm_store_ss((float*)(dst+dst_skip), (__m128)shuffled1);
  528. _mm_store_ss((float*)(dst+2*dst_skip), (__m128)shuffled2);
  529. _mm_store_ss((float*)(dst+3*dst_skip), (__m128)shuffled3);
  530. #endif
  531. dst += 4*dst_skip;
  532. src+= 4;
  533. }
  534. while (nsamples--) {
  535. __m128 in = _mm_load_ss(src);
  536. __m128 scaled = _mm_mul_ss(in, factor);
  537. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  538. int y = _mm_cvttss_si32(clipped);
  539. *((int *) dst) = y<<8;
  540. dst += dst_skip;
  541. src++;
  542. }
  543. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  544. unsigned long unrolled = nsamples / 4;
  545. nsamples = nsamples & 3;
  546. while (unrolled--) {
  547. float32x4_t samples = vld1q_f32(src);
  548. int32x4_t converted = float_24_neon(samples);
  549. switch(dst_skip) {
  550. case 4:
  551. vst1q_s32((int32_t*)dst, converted);
  552. break;
  553. default:
  554. vst1q_lane_s32((int32_t*)(dst), converted, 0);
  555. vst1q_lane_s32((int32_t*)(dst+dst_skip), converted, 1);
  556. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), converted, 2);
  557. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), converted, 3);
  558. break;
  559. }
  560. dst += 4*dst_skip;
  561. src+= 4;
  562. }
  563. #endif
  564. #if !defined (__SSE2__)
  565. while (nsamples--) {
  566. float_24l32 (*src, *((int32_t*) dst));
  567. dst += dst_skip;
  568. src++;
  569. }
  570. #endif
  571. }
  572. void sample_move_dS_s32l24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  573. {
  574. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  575. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  576. unsigned long unrolled = nsamples / 4;
  577. while (unrolled--) {
  578. uint32x4_t src128;
  579. switch(src_skip)
  580. {
  581. case 4:
  582. src128 = vld1q_u32((uint32_t*)src);
  583. break;
  584. case 8:
  585. src128 = vld2q_u32((uint32_t*)src).val[0];
  586. break;
  587. default:
  588. src128 = vld1q_lane_u32((uint32_t*)src, src128, 0);
  589. src128 = vld1q_lane_u32((uint32_t*)(src+src_skip), src128, 1);
  590. src128 = vld1q_lane_u32((uint32_t*)(src+2*src_skip), src128, 2);
  591. src128 = vld1q_lane_u32((uint32_t*)(src+3*src_skip), src128, 3);
  592. break;
  593. }
  594. src128 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(src128)));
  595. uint32x4_t toupper = vshlq_n_u32(src128, 8);
  596. int32x4_t shifted = vshrq_n_s32((int32x4_t)toupper, 8);
  597. float32x4_t as_float = vcvtq_f32_s32(shifted);
  598. float32x4_t divided = vmulq_f32(as_float, factor);
  599. vst1q_f32(dst, divided);
  600. src += 4*src_skip;
  601. dst += 4;
  602. }
  603. nsamples = nsamples & 3;
  604. #endif
  605. /* ALERT: signed sign-extension portability !!! */
  606. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  607. while (nsamples--) {
  608. int32_t x;
  609. #if __BYTE_ORDER == __LITTLE_ENDIAN
  610. x = (unsigned char)(src[0]);
  611. x <<= 8;
  612. x |= (unsigned char)(src[1]);
  613. x <<= 8;
  614. x |= (unsigned char)(src[2]);
  615. x <<= 8;
  616. x |= (unsigned char)(src[3]);
  617. #elif __BYTE_ORDER == __BIG_ENDIAN
  618. x = (unsigned char)(src[3]);
  619. x <<= 8;
  620. x |= (unsigned char)(src[2]);
  621. x <<= 8;
  622. x |= (unsigned char)(src[1]);
  623. x <<= 8;
  624. x |= (unsigned char)(src[0]);
  625. #endif
  626. *dst = (x >> 0) * scaling;
  627. dst++;
  628. src += src_skip;
  629. }
  630. }
  631. void sample_move_dS_s32l24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  632. {
  633. #if defined (__SSE2__) && !defined (__sun__)
  634. unsigned long unrolled = nsamples / 4;
  635. static float inv_sample_max_24bit = 1.0 / SAMPLE_24BIT_SCALING;
  636. __m128 factor = _mm_set1_ps(inv_sample_max_24bit);
  637. while (unrolled--)
  638. {
  639. int i1 = *((int *) src);
  640. src+= src_skip;
  641. int i2 = *((int *) src);
  642. src+= src_skip;
  643. int i3 = *((int *) src);
  644. src+= src_skip;
  645. int i4 = *((int *) src);
  646. src+= src_skip;
  647. __m128i shifted = _mm_set_epi32(i4, i3, i2, i1);
  648. __m128 as_float = _mm_cvtepi32_ps(shifted);
  649. __m128 divided = _mm_mul_ps(as_float, factor);
  650. _mm_storeu_ps(dst, divided);
  651. dst += 4;
  652. }
  653. nsamples = nsamples & 3;
  654. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  655. unsigned long unrolled = nsamples / 4;
  656. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  657. while (unrolled--) {
  658. uint32x4_t src128;
  659. switch(src_skip) {
  660. case 4:
  661. src128 = vld1q_u32((uint32_t*)src);
  662. break;
  663. case 8:
  664. src128 = vld2q_u32((uint32_t*)src).val[0];
  665. break;
  666. default:
  667. src128 = vld1q_lane_u32((uint32_t*)src, src128, 0);
  668. src128 = vld1q_lane_u32((uint32_t*)(src+src_skip), src128, 1);
  669. src128 = vld1q_lane_u32((uint32_t*)(src+2*src_skip), src128, 2);
  670. src128 = vld1q_lane_u32((uint32_t*)(src+3*src_skip), src128, 3);
  671. break;
  672. }
  673. // Sign extension by moving to upper as unsigned, then down
  674. uint32x4_t toupper = vshlq_n_u32(src128, 8);
  675. int32x4_t shifted = vshrq_n_s32((int32x4_t)toupper, 8);
  676. float32x4_t as_float = vcvtq_f32_s32(shifted);
  677. float32x4_t divided = vmulq_f32(as_float, factor);
  678. vst1q_f32(dst, divided);
  679. src += 4*src_skip;
  680. dst += 4;
  681. }
  682. nsamples = nsamples & 3;
  683. #endif
  684. /* ALERT: signed sign-extension portability !!! */
  685. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  686. while (nsamples--) {
  687. uint32_t val=(*((uint32_t*)src));
  688. if (val & 0x800000u) val|=0xFF000000u;
  689. *dst = (*((int32_t *) &val)) * scaling;
  690. dst++;
  691. src += src_skip;
  692. }
  693. }
  694. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  695. {
  696. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  697. unsigned long unrolled = nsamples / 4;
  698. while (unrolled--) {
  699. int i;
  700. int32_t z[4];
  701. float32x4_t samples = vld1q_f32(src);
  702. int32x4_t converted = float_24_neon(samples);
  703. converted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(converted)));
  704. vst1q_s32(z, converted);
  705. for (i = 0; i != 4; ++i) {
  706. memcpy (dst, ((char*)(z+i))+1, 3);
  707. dst += dst_skip;
  708. }
  709. src += 4;
  710. }
  711. nsamples = nsamples & 3;
  712. #endif
  713. int32_t z;
  714. while (nsamples--) {
  715. float_24 (*src, z);
  716. #if __BYTE_ORDER == __LITTLE_ENDIAN
  717. dst[0]=(char)(z>>16);
  718. dst[1]=(char)(z>>8);
  719. dst[2]=(char)(z);
  720. #elif __BYTE_ORDER == __BIG_ENDIAN
  721. dst[0]=(char)(z);
  722. dst[1]=(char)(z>>8);
  723. dst[2]=(char)(z>>16);
  724. #endif
  725. dst += dst_skip;
  726. src++;
  727. }
  728. }
  729. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  730. {
  731. #if defined (__SSE2__) && !defined (__sun__)
  732. _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
  733. while (nsamples >= 4) {
  734. int i;
  735. int32_t z[4];
  736. __m128 samples = _mm_loadu_ps(src);
  737. __m128i converted = float_24_sse(samples);
  738. #ifdef __SSE4_1__
  739. z[0] = _mm_extract_epi32(converted, 0);
  740. z[1] = _mm_extract_epi32(converted, 1);
  741. z[2] = _mm_extract_epi32(converted, 2);
  742. z[3] = _mm_extract_epi32(converted, 3);
  743. #else
  744. __m128i shuffled1 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(0, 3, 2, 1));
  745. __m128i shuffled2 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(1, 0, 3, 2));
  746. __m128i shuffled3 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(2, 1, 0, 3));
  747. _mm_store_ss((float*)z, (__m128)converted);
  748. _mm_store_ss((float*)z+1, (__m128)shuffled1);
  749. _mm_store_ss((float*)z+2, (__m128)shuffled2);
  750. _mm_store_ss((float*)z+3, (__m128)shuffled3);
  751. #endif
  752. for (i = 0; i != 4; ++i) {
  753. memcpy (dst, z+i, 3);
  754. dst += dst_skip;
  755. }
  756. nsamples -= 4;
  757. src += 4;
  758. }
  759. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  760. unsigned long unrolled = nsamples / 4;
  761. while (unrolled--) {
  762. int i;
  763. int32_t z[4];
  764. float32x4_t samples = vld1q_f32(src);
  765. int32x4_t converted = float_24_neon(samples);
  766. vst1q_s32(z, converted);
  767. for (i = 0; i != 4; ++i) {
  768. memcpy (dst, z+i, 3);
  769. dst += dst_skip;
  770. }
  771. src += 4;
  772. }
  773. nsamples = nsamples & 3;
  774. #endif
  775. int32_t z;
  776. while (nsamples--) {
  777. float_24 (*src, z);
  778. #if __BYTE_ORDER == __LITTLE_ENDIAN
  779. memcpy (dst, &z, 3);
  780. #elif __BYTE_ORDER == __BIG_ENDIAN
  781. memcpy (dst, (char *)&z + 1, 3);
  782. #endif
  783. dst += dst_skip;
  784. src++;
  785. }
  786. }
  787. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  788. {
  789. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  790. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  791. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  792. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  793. int32_t x[4];
  794. memset(x, 0, sizeof(x));
  795. unsigned long unrolled = nsamples / 4;
  796. while (unrolled--) {
  797. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  798. // right aligned / inverse sequence below -> *256
  799. memcpy(((char*)&x[0])+1, src, 3);
  800. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  801. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  802. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  803. #else
  804. memcpy(&x[0], src, 3);
  805. memcpy(&x[1], src+src_skip, 3);
  806. memcpy(&x[2], src+2*src_skip, 3);
  807. memcpy(&x[3], src+3*src_skip, 3);
  808. #endif
  809. src += 4 * src_skip;
  810. int32x4_t source = vld1q_s32(x);
  811. source = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(source)));
  812. float32x4_t converted = vcvtq_f32_s32(source);
  813. float32x4_t scaled = vmulq_f32(converted, vscaling);
  814. vst1q_f32(dst, scaled);
  815. dst += 4;
  816. }
  817. nsamples = nsamples & 3;
  818. #endif
  819. /* ALERT: signed sign-extension portability !!! */
  820. while (nsamples--) {
  821. int x;
  822. #if __BYTE_ORDER == __LITTLE_ENDIAN
  823. x = (unsigned char)(src[0]);
  824. x <<= 8;
  825. x |= (unsigned char)(src[1]);
  826. x <<= 8;
  827. x |= (unsigned char)(src[2]);
  828. /* correct sign bit and the rest of the top byte */
  829. if (src[0] & 0x80) {
  830. x |= 0xff << 24;
  831. }
  832. #elif __BYTE_ORDER == __BIG_ENDIAN
  833. x = (unsigned char)(src[2]);
  834. x <<= 8;
  835. x |= (unsigned char)(src[1]);
  836. x <<= 8;
  837. x |= (unsigned char)(src[0]);
  838. /* correct sign bit and the rest of the top byte */
  839. if (src[2] & 0x80) {
  840. x |= 0xff << 24;
  841. }
  842. #endif
  843. *dst = x * scaling;
  844. dst++;
  845. src += src_skip;
  846. }
  847. }
  848. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  849. {
  850. const jack_default_audio_sample_t scaling = 1.f/SAMPLE_24BIT_SCALING;
  851. #if defined (__SSE2__) && !defined (__sun__)
  852. const __m128 scaling_block = _mm_set_ps1(scaling);
  853. while (nsamples >= 4) {
  854. int x0, x1, x2, x3;
  855. memcpy((char*)&x0 + 1, src, 3);
  856. memcpy((char*)&x1 + 1, src+src_skip, 3);
  857. memcpy((char*)&x2 + 1, src+2*src_skip, 3);
  858. memcpy((char*)&x3 + 1, src+3*src_skip, 3);
  859. src += 4 * src_skip;
  860. const __m128i block_i = _mm_set_epi32(x3, x2, x1, x0);
  861. const __m128i shifted = _mm_srai_epi32(block_i, 8);
  862. const __m128 converted = _mm_cvtepi32_ps (shifted);
  863. const __m128 scaled = _mm_mul_ps(converted, scaling_block);
  864. _mm_storeu_ps(dst, scaled);
  865. dst += 4;
  866. nsamples -= 4;
  867. }
  868. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  869. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  870. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  871. int32_t x[4];
  872. memset(x, 0, sizeof(x));
  873. unsigned long unrolled = nsamples / 4;
  874. while (unrolled--) {
  875. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  876. // left aligned -> *256
  877. memcpy(&x[0], src, 3);
  878. memcpy(&x[1], src+src_skip, 3);
  879. memcpy(&x[2], src+2*src_skip, 3);
  880. memcpy(&x[3], src+3*src_skip, 3);
  881. #else
  882. memcpy(((char*)&x[0])+1, src, 3);
  883. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  884. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  885. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  886. #endif
  887. src += 4 * src_skip;
  888. int32x4_t source = vld1q_s32(x);
  889. float32x4_t converted = vcvtq_f32_s32(source);
  890. float32x4_t scaled = vmulq_f32(converted, vscaling);
  891. vst1q_f32(dst, scaled);
  892. dst += 4;
  893. }
  894. nsamples = nsamples & 3;
  895. #endif
  896. while (nsamples--) {
  897. int x;
  898. #if __BYTE_ORDER == __LITTLE_ENDIAN
  899. memcpy((char*)&x + 1, src, 3);
  900. #elif __BYTE_ORDER == __BIG_ENDIAN
  901. memcpy(&x, src, 3);
  902. #endif
  903. x >>= 8;
  904. *dst = x * scaling;
  905. dst++;
  906. src += src_skip;
  907. }
  908. }
  909. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  910. {
  911. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  912. unsigned long unrolled = nsamples / 4;
  913. nsamples = nsamples & 3;
  914. while (unrolled--) {
  915. float32x4_t samples = vld1q_f32(src);
  916. int16x4_t converted = float_16_neon(samples);
  917. converted = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(converted)));
  918. switch(dst_skip) {
  919. case 2:
  920. vst1_s16((int16_t*)dst, converted);
  921. break;
  922. default:
  923. vst1_lane_s16((int16_t*)(dst), converted, 0);
  924. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  925. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  926. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  927. break;
  928. }
  929. dst += 4*dst_skip;
  930. src+= 4;
  931. }
  932. #endif
  933. int16_t tmp;
  934. while (nsamples--) {
  935. // float_16 (*src, tmp);
  936. if (*src <= NORMALIZED_FLOAT_MIN) {
  937. tmp = SAMPLE_16BIT_MIN;
  938. } else if (*src >= NORMALIZED_FLOAT_MAX) {
  939. tmp = SAMPLE_16BIT_MAX;
  940. } else {
  941. tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
  942. }
  943. #if __BYTE_ORDER == __LITTLE_ENDIAN
  944. dst[0]=(char)(tmp>>8);
  945. dst[1]=(char)(tmp);
  946. #elif __BYTE_ORDER == __BIG_ENDIAN
  947. dst[0]=(char)(tmp);
  948. dst[1]=(char)(tmp>>8);
  949. #endif
  950. dst += dst_skip;
  951. src++;
  952. }
  953. }
  954. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  955. {
  956. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  957. unsigned long unrolled = nsamples / 4;
  958. nsamples = nsamples & 3;
  959. while (unrolled--) {
  960. float32x4_t samples = vld1q_f32(src);
  961. int16x4_t converted = float_16_neon(samples);
  962. switch(dst_skip) {
  963. case 2:
  964. vst1_s16((int16_t*)dst, converted);
  965. break;
  966. default:
  967. vst1_lane_s16((int16_t*)(dst), converted, 0);
  968. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  969. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  970. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  971. break;
  972. }
  973. dst += 4*dst_skip;
  974. src+= 4;
  975. }
  976. #endif
  977. while (nsamples--) {
  978. float_16 (*src, *((int16_t*) dst));
  979. dst += dst_skip;
  980. src++;
  981. }
  982. }
  983. void sample_move_dither_rect_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  984. {
  985. jack_default_audio_sample_t val;
  986. int16_t tmp;
  987. while (nsamples--) {
  988. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float) UINT_MAX - 0.5f;
  989. float_16_scaled (val, tmp);
  990. #if __BYTE_ORDER == __LITTLE_ENDIAN
  991. dst[0]=(char)(tmp>>8);
  992. dst[1]=(char)(tmp);
  993. #elif __BYTE_ORDER == __BIG_ENDIAN
  994. dst[0]=(char)(tmp);
  995. dst[1]=(char)(tmp>>8);
  996. #endif
  997. dst += dst_skip;
  998. src++;
  999. }
  1000. }
  1001. void sample_move_dither_rect_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  1002. {
  1003. jack_default_audio_sample_t val;
  1004. while (nsamples--) {
  1005. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float)UINT_MAX - 0.5f;
  1006. float_16_scaled (val, *((int16_t*) dst));
  1007. dst += dst_skip;
  1008. src++;
  1009. }
  1010. }
  1011. void sample_move_dither_tri_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  1012. {
  1013. jack_default_audio_sample_t val;
  1014. int16_t tmp;
  1015. while (nsamples--) {
  1016. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  1017. float_16_scaled (val, tmp);
  1018. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1019. dst[0]=(char)(tmp>>8);
  1020. dst[1]=(char)(tmp);
  1021. #elif __BYTE_ORDER == __BIG_ENDIAN
  1022. dst[0]=(char)(tmp);
  1023. dst[1]=(char)(tmp>>8);
  1024. #endif
  1025. dst += dst_skip;
  1026. src++;
  1027. }
  1028. }
  1029. void sample_move_dither_tri_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  1030. {
  1031. jack_default_audio_sample_t val;
  1032. while (nsamples--) {
  1033. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  1034. float_16_scaled (val, *((int16_t*) dst));
  1035. dst += dst_skip;
  1036. src++;
  1037. }
  1038. }
  1039. void sample_move_dither_shaped_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  1040. {
  1041. jack_default_audio_sample_t x;
  1042. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  1043. jack_default_audio_sample_t xp; /* x' */
  1044. float r;
  1045. float rm1 = state->rm1;
  1046. unsigned int idx = state->idx;
  1047. int16_t tmp;
  1048. while (nsamples--) {
  1049. x = *src * SAMPLE_16BIT_SCALING;
  1050. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  1051. /* Filter the error with Lipshitz's minimally audible FIR:
  1052. [2.033 -2.165 1.959 -1.590 0.6149] */
  1053. xe = x
  1054. - state->e[idx] * 2.033f
  1055. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  1056. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  1057. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  1058. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  1059. xp = xe + r - rm1;
  1060. rm1 = r;
  1061. float_16_scaled (xp, tmp);
  1062. /* Intrinsic z^-1 delay */
  1063. idx = (idx + 1) & DITHER_BUF_MASK;
  1064. state->e[idx] = xp - xe;
  1065. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1066. dst[0]=(char)(tmp>>8);
  1067. dst[1]=(char)(tmp);
  1068. #elif __BYTE_ORDER == __BIG_ENDIAN
  1069. dst[0]=(char)(tmp);
  1070. dst[1]=(char)(tmp>>8);
  1071. #endif
  1072. dst += dst_skip;
  1073. src++;
  1074. }
  1075. state->rm1 = rm1;
  1076. state->idx = idx;
  1077. }
  1078. void sample_move_dither_shaped_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  1079. {
  1080. jack_default_audio_sample_t x;
  1081. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  1082. jack_default_audio_sample_t xp; /* x' */
  1083. float r;
  1084. float rm1 = state->rm1;
  1085. unsigned int idx = state->idx;
  1086. while (nsamples--) {
  1087. x = *src * SAMPLE_16BIT_SCALING;
  1088. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  1089. /* Filter the error with Lipshitz's minimally audible FIR:
  1090. [2.033 -2.165 1.959 -1.590 0.6149] */
  1091. xe = x
  1092. - state->e[idx] * 2.033f
  1093. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  1094. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  1095. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  1096. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  1097. xp = xe + r - rm1;
  1098. rm1 = r;
  1099. float_16_scaled (xp, *((int16_t*) dst));
  1100. /* Intrinsic z^-1 delay */
  1101. idx = (idx + 1) & DITHER_BUF_MASK;
  1102. state->e[idx] = *((int16_t*) dst) - xe;
  1103. dst += dst_skip;
  1104. src++;
  1105. }
  1106. state->rm1 = rm1;
  1107. state->idx = idx;
  1108. }
  1109. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  1110. {
  1111. short z;
  1112. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  1113. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  1114. const float32x4_t vscaling = vdupq_n_f32(scaling);
  1115. unsigned long unrolled = nsamples / 4;
  1116. while (unrolled--) {
  1117. int16x4_t source16x4;
  1118. switch(src_skip) {
  1119. case 2:
  1120. source16x4 = vld1_s16((int16_t*)src);
  1121. break;
  1122. case 4:
  1123. source16x4 = vld2_s16((int16_t*)src).val[0];
  1124. break;
  1125. default:
  1126. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  1127. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  1128. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  1129. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  1130. break;
  1131. }
  1132. source16x4 = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(source16x4)));
  1133. int32x4_t source32x4 = vmovl_s16(source16x4);
  1134. src += 4 * src_skip;
  1135. float32x4_t converted = vcvtq_f32_s32(source32x4);
  1136. float32x4_t scaled = vmulq_f32(converted, vscaling);
  1137. vst1q_f32(dst, scaled);
  1138. dst += 4;
  1139. }
  1140. nsamples = nsamples & 3;
  1141. #endif
  1142. /* ALERT: signed sign-extension portability !!! */
  1143. while (nsamples--) {
  1144. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1145. z = (unsigned char)(src[0]);
  1146. z <<= 8;
  1147. z |= (unsigned char)(src[1]);
  1148. #elif __BYTE_ORDER == __BIG_ENDIAN
  1149. z = (unsigned char)(src[1]);
  1150. z <<= 8;
  1151. z |= (unsigned char)(src[0]);
  1152. #endif
  1153. *dst = z * scaling;
  1154. dst++;
  1155. src += src_skip;
  1156. }
  1157. }
  1158. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  1159. {
  1160. /* ALERT: signed sign-extension portability !!! */
  1161. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  1162. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  1163. const float32x4_t vscaling = vdupq_n_f32(scaling);
  1164. unsigned long unrolled = nsamples / 4;
  1165. while (unrolled--) {
  1166. int16x4_t source16x4;
  1167. switch(src_skip) {
  1168. case 2:
  1169. source16x4 = vld1_s16((int16_t*)src);
  1170. break;
  1171. case 4:
  1172. source16x4 = vld2_s16((int16_t*)src).val[0];
  1173. break;
  1174. default:
  1175. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  1176. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  1177. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  1178. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  1179. break;
  1180. }
  1181. int32x4_t source32x4 = vmovl_s16(source16x4);
  1182. src += 4 * src_skip;
  1183. float32x4_t converted = vcvtq_f32_s32(source32x4);
  1184. float32x4_t scaled = vmulq_f32(converted, vscaling);
  1185. vst1q_f32(dst, scaled);
  1186. dst += 4;
  1187. }
  1188. nsamples = nsamples & 3;
  1189. #endif
  1190. while (nsamples--) {
  1191. *dst = (*((short *) src)) * scaling;
  1192. dst++;
  1193. src += src_skip;
  1194. }
  1195. }
  1196. void memset_interleave (char *dst, char val, unsigned long bytes,
  1197. unsigned long unit_bytes,
  1198. unsigned long skip_bytes)
  1199. {
  1200. switch (unit_bytes) {
  1201. case 1:
  1202. while (bytes--) {
  1203. *dst = val;
  1204. dst += skip_bytes;
  1205. }
  1206. break;
  1207. case 2:
  1208. while (bytes) {
  1209. *((short *) dst) = (short) val;
  1210. dst += skip_bytes;
  1211. bytes -= 2;
  1212. }
  1213. break;
  1214. case 4:
  1215. while (bytes) {
  1216. *((int *) dst) = (int) val;
  1217. dst += skip_bytes;
  1218. bytes -= 4;
  1219. }
  1220. break;
  1221. default:
  1222. while (bytes) {
  1223. memset(dst, val, unit_bytes);
  1224. dst += skip_bytes;
  1225. bytes -= unit_bytes;
  1226. }
  1227. break;
  1228. }
  1229. }
  1230. /* COPY FUNCTIONS: used to move data from an input channel to an
  1231. output channel. Note that we assume that the skip distance
  1232. is the same for both channels. This is completely fine
  1233. unless the input and output were on different audio interfaces that
  1234. were interleaved differently. We don't try to handle that.
  1235. */
  1236. void
  1237. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  1238. {
  1239. memcpy (dst, src, src_bytes);
  1240. }
  1241. void
  1242. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  1243. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1244. {
  1245. while (src_bytes) {
  1246. *((short *) dst) = *((short *) src);
  1247. dst += dst_skip_bytes;
  1248. src += src_skip_bytes;
  1249. src_bytes -= 2;
  1250. }
  1251. }
  1252. void
  1253. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  1254. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1255. {
  1256. while (src_bytes) {
  1257. memcpy(dst, src, 3);
  1258. dst += dst_skip_bytes;
  1259. src += src_skip_bytes;
  1260. src_bytes -= 3;
  1261. }
  1262. }
  1263. void
  1264. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1265. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1266. {
  1267. while (src_bytes) {
  1268. *((int *) dst) = *((int *) src);
  1269. dst += dst_skip_bytes;
  1270. src += src_skip_bytes;
  1271. src_bytes -= 4;
  1272. }
  1273. }