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.

1172 lines
32KB

  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 maxium 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
  60. #define SAMPLE_16BIT_SCALING 32767
  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;\
  105. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  106. (d) = SAMPLE_24BIT_MAX;\
  107. } else {\
  108. (d) = f_round ((s) * SAMPLE_24BIT_SCALING);\
  109. }
  110. #define float_24(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. #if defined (__SSE2__) && !defined (__sun__)
  119. /* generates same as _mm_set_ps(1.f, 1.f, 1f., 1f) but faster */
  120. static inline __m128 gen_one(void)
  121. {
  122. volatile __m128i x = { 0 }; /* shut up, GCC */
  123. __m128i ones = _mm_cmpeq_epi32(x, x);
  124. return (__m128)_mm_slli_epi32 (_mm_srli_epi32(ones, 25), 23);
  125. }
  126. static inline __m128 clip(__m128 s, __m128 min, __m128 max)
  127. {
  128. return _mm_min_ps(max, _mm_max_ps(s, min));
  129. }
  130. static inline __m128i float_24_sse(__m128 s)
  131. {
  132. const __m128 upper_bound = gen_one(); /* NORMALIZED_FLOAT_MAX */
  133. const __m128 lower_bound = _mm_sub_ps(_mm_setzero_ps(), upper_bound);
  134. __m128 clipped = clip(s, lower_bound, upper_bound);
  135. __m128 scaled = _mm_mul_ps(clipped, _mm_set1_ps(SAMPLE_24BIT_SCALING));
  136. return _mm_cvtps_epi32(scaled);
  137. }
  138. #endif
  139. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  140. static inline float32x4_t clip(float32x4_t s, float32x4_t min, float32x4_t max)
  141. {
  142. return vminq_f32(max, vmaxq_f32(s, min));
  143. }
  144. static inline int32x4_t float_24_neon(float32x4_t s)
  145. {
  146. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  147. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  148. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  149. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_24BIT_SCALING));
  150. return vcvtq_s32_f32(scaled);
  151. }
  152. static inline int16x4_t float_16_neon(float32x4_t s)
  153. {
  154. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  155. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  156. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  157. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_16BIT_SCALING));
  158. return vmovn_s32(vcvtq_s32_f32(scaled));
  159. }
  160. #endif
  161. /* Linear Congruential noise generator. From the music-dsp list
  162. * less random than rand(), but good enough and 10x faster
  163. */
  164. static unsigned int seed = 22222;
  165. static inline unsigned int fast_rand() {
  166. seed = (seed * 196314165) + 907633515;
  167. return seed;
  168. }
  169. /* functions for native float sample data */
  170. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  171. while (nsamples--) {
  172. *dst = *((float *) src);
  173. dst++;
  174. src += src_skip;
  175. }
  176. }
  177. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  178. while (nsamples--) {
  179. *((float *) dst) = *src;
  180. dst += dst_skip;
  181. src++;
  182. }
  183. }
  184. /* NOTES on function naming:
  185. foo_bar_d<TYPE>_s<TYPE>
  186. the "d<TYPE>" component defines the destination type for the operation
  187. the "s<TYPE>" component defines the source type for the operation
  188. TYPE can be one of:
  189. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  190. Ss - like S but reverse endian from the host CPU
  191. 32u24 - sample is an signed 32 bit integer value, but data is in lower 24 bits only
  192. 32u24s - like 32u24 but reverse endian from the host CPU
  193. 24 - sample is an signed 24 bit integer value
  194. 24s - like 24 but reverse endian from the host CPU
  195. 16 - sample is an signed 16 bit integer value
  196. 16s - like 16 but reverse endian from the host CPU
  197. For obvious reasons, the reverse endian versions only show as source types.
  198. This covers all known sample formats at 16 bits or larger.
  199. */
  200. /* functions for native integer sample data */
  201. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  202. {
  203. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  204. unsigned long unrolled = nsamples / 4;
  205. nsamples = nsamples & 3;
  206. while (unrolled--) {
  207. float32x4_t samples = vld1q_f32(src);
  208. int32x4_t converted = float_24_neon(samples);
  209. converted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(converted)));
  210. switch(dst_skip) {
  211. case 4:
  212. vst1q_s32((int32_t*)dst, converted);
  213. break;
  214. default:
  215. vst1q_lane_s32((int32_t*)(dst), converted, 0);
  216. vst1q_lane_s32((int32_t*)(dst+dst_skip), converted, 1);
  217. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), converted, 2);
  218. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), converted, 3);
  219. break;
  220. }
  221. dst += 4*dst_skip;
  222. src+= 4;
  223. }
  224. #endif
  225. int32_t z;
  226. while (nsamples--) {
  227. float_24u32 (*src, z);
  228. #if __BYTE_ORDER == __LITTLE_ENDIAN
  229. dst[0]=(char)(z>>24);
  230. dst[1]=(char)(z>>16);
  231. dst[2]=(char)(z>>8);
  232. dst[3]=(char)(z);
  233. #elif __BYTE_ORDER == __BIG_ENDIAN
  234. dst[0]=(char)(z);
  235. dst[1]=(char)(z>>8);
  236. dst[2]=(char)(z>>16);
  237. dst[3]=(char)(z>>24);
  238. #endif
  239. dst += dst_skip;
  240. src++;
  241. }
  242. }
  243. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  244. {
  245. #if defined (__SSE2__) && !defined (__sun__)
  246. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  247. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  248. __m128 factor = int_max;
  249. unsigned long unrolled = nsamples / 4;
  250. nsamples = nsamples & 3;
  251. while (unrolled--) {
  252. __m128 in = _mm_load_ps(src);
  253. __m128 scaled = _mm_mul_ps(in, factor);
  254. __m128 clipped = clip(scaled, int_min, int_max);
  255. __m128i y = _mm_cvttps_epi32(clipped);
  256. #ifdef __SSE4_1__
  257. *(int32_t*)dst = _mm_extract_epi32(y, 0);
  258. *(int32_t*)(dst+dst_skip) = _mm_extract_epi32(y, 1);
  259. *(int32_t*)(dst+2*dst_skip) = _mm_extract_epi32(y, 2);
  260. *(int32_t*)(dst+3*dst_skip) = _mm_extract_epi32(y, 3);
  261. #else
  262. __m128i shuffled1 = _mm_shuffle_epi32(y, _MM_SHUFFLE(0, 3, 2, 1));
  263. __m128i shuffled2 = _mm_shuffle_epi32(y, _MM_SHUFFLE(1, 0, 3, 2));
  264. __m128i shuffled3 = _mm_shuffle_epi32(y, _MM_SHUFFLE(2, 1, 0, 3));
  265. _mm_store_ss((float*)dst, (__m128)y);
  266. _mm_store_ss((float*)(dst+dst_skip), (__m128)shuffled1);
  267. _mm_store_ss((float*)(dst+2*dst_skip), (__m128)shuffled2);
  268. _mm_store_ss((float*)(dst+3*dst_skip), (__m128)shuffled3);
  269. #endif
  270. dst += 4*dst_skip;
  271. src+= 4;
  272. }
  273. while (nsamples--) {
  274. __m128 in = _mm_load_ss(src);
  275. __m128 scaled = _mm_mul_ss(in, factor);
  276. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  277. int y = _mm_cvttss_si32(clipped);
  278. *((int *) dst) = y;
  279. dst += dst_skip;
  280. src++;
  281. }
  282. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  283. unsigned long unrolled = nsamples / 4;
  284. nsamples = nsamples & 3;
  285. while (unrolled--) {
  286. float32x4_t samples = vld1q_f32(src);
  287. int32x4_t converted = float_24_neon(samples);
  288. switch(dst_skip) {
  289. case 4:
  290. vst1q_s32((int32_t*)dst, converted);
  291. break;
  292. default:
  293. vst1q_lane_s32((int32_t*)(dst), converted, 0);
  294. vst1q_lane_s32((int32_t*)(dst+dst_skip), converted, 1);
  295. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), converted, 2);
  296. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), converted, 3);
  297. break;
  298. }
  299. dst += 4*dst_skip;
  300. src+= 4;
  301. }
  302. #endif
  303. #if !defined (__SSE2__)
  304. while (nsamples--) {
  305. float_24u32 (*src, *((int32_t*) dst));
  306. dst += dst_skip;
  307. src++;
  308. }
  309. #endif
  310. }
  311. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  312. {
  313. const jack_default_audio_sample_t scaling = 1.0 / (SAMPLE_24BIT_SCALING << 8);
  314. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  315. float32x4_t factor = vdupq_n_f32(scaling);
  316. unsigned long unrolled = nsamples / 4;
  317. while (unrolled--) {
  318. int32x4_t src128;
  319. switch(src_skip)
  320. {
  321. case 4:
  322. src128 = vld1q_s32((int32_t*)src);
  323. break;
  324. case 8:
  325. src128 = vld2q_s32((int32_t*)src).val[0];
  326. break;
  327. default:
  328. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  329. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  330. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  331. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  332. break;
  333. }
  334. src128 = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(src128)));
  335. /* sign extension - left shift will be reverted by scaling */
  336. int32x4_t shifted = vshlq_n_s32(src128, 8);
  337. float32x4_t as_float = vcvtq_f32_s32(shifted);
  338. float32x4_t divided = vmulq_f32(as_float, factor);
  339. vst1q_f32(dst, divided);
  340. src += 4*src_skip;
  341. dst += 4;
  342. }
  343. nsamples = nsamples & 3;
  344. #endif
  345. /* ALERT: signed sign-extension portability !!! */
  346. while (nsamples--) {
  347. int x;
  348. #if __BYTE_ORDER == __LITTLE_ENDIAN
  349. x = (unsigned char)(src[0]);
  350. x <<= 8;
  351. x |= (unsigned char)(src[1]);
  352. x <<= 8;
  353. x |= (unsigned char)(src[2]);
  354. x <<= 8;
  355. x |= (unsigned char)(src[3]);
  356. #elif __BYTE_ORDER == __BIG_ENDIAN
  357. x = (unsigned char)(src[3]);
  358. x <<= 8;
  359. x |= (unsigned char)(src[2]);
  360. x <<= 8;
  361. x |= (unsigned char)(src[1]);
  362. x <<= 8;
  363. x |= (unsigned char)(src[0]);
  364. #endif
  365. /* sign extension - left shift will be reverted by scaling */
  366. *dst = (x << 8) * scaling;
  367. dst++;
  368. src += src_skip;
  369. }
  370. }
  371. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  372. {
  373. const jack_default_audio_sample_t scaling = 1.0 / (SAMPLE_24BIT_SCALING << 8);
  374. #if defined (__SSE2__) && !defined (__sun__)
  375. unsigned long unrolled = nsamples / 4;
  376. __m128 factor = _mm_set1_ps(scaling);
  377. while (unrolled--)
  378. {
  379. int i1 = *((int *) src);
  380. src+= src_skip;
  381. int i2 = *((int *) src);
  382. src+= src_skip;
  383. int i3 = *((int *) src);
  384. src+= src_skip;
  385. int i4 = *((int *) src);
  386. src+= src_skip;
  387. __m128i src = _mm_set_epi32(i4, i3, i2, i1);
  388. /* sign extension - left shift will be reverted by scaling */
  389. __m128i shifted = _mm_slli_epi32(src, 8);
  390. __m128 as_float = _mm_cvtepi32_ps(shifted);
  391. __m128 divided = _mm_mul_ps(as_float, factor);
  392. _mm_storeu_ps(dst, divided);
  393. dst += 4;
  394. }
  395. nsamples = nsamples & 3;
  396. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  397. unsigned long unrolled = nsamples / 4;
  398. float32x4_t factor = vdupq_n_f32(scaling);
  399. while (unrolled--) {
  400. int32x4_t src128;
  401. switch(src_skip) {
  402. case 4:
  403. src128 = vld1q_s32((int32_t*)src);
  404. break;
  405. case 8:
  406. src128 = vld2q_s32((int32_t*)src).val[0];
  407. break;
  408. default:
  409. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  410. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  411. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  412. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  413. break;
  414. }
  415. /* sign extension - left shift will be reverted by scaling */
  416. int32x4_t shifted = vshlq_n_s32(src128, 8);
  417. float32x4_t as_float = vcvtq_f32_s32(shifted);
  418. float32x4_t divided = vmulq_f32(as_float, factor);
  419. vst1q_f32(dst, divided);
  420. src += 4*src_skip;
  421. dst += 4;
  422. }
  423. nsamples = nsamples & 3;
  424. #endif
  425. /* ALERT: signed sign-extension portability !!! */
  426. while (nsamples--) {
  427. /* sign extension - left shift will be reverted by scaling */
  428. *dst = (*((int *) src) << 8) * scaling;
  429. dst++;
  430. src += src_skip;
  431. }
  432. }
  433. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  434. {
  435. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  436. unsigned long unrolled = nsamples / 4;
  437. while (unrolled--) {
  438. int i;
  439. int32_t z[4];
  440. float32x4_t samples = vld1q_f32(src);
  441. int32x4_t converted = float_24_neon(samples);
  442. converted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(converted)));
  443. vst1q_s32(z, converted);
  444. for (i = 0; i != 4; ++i) {
  445. memcpy (dst, ((char*)(z+i))+1, 3);
  446. dst += dst_skip;
  447. }
  448. src += 4;
  449. }
  450. nsamples = nsamples & 3;
  451. #endif
  452. int32_t z;
  453. while (nsamples--) {
  454. float_24 (*src, z);
  455. #if __BYTE_ORDER == __LITTLE_ENDIAN
  456. dst[0]=(char)(z>>16);
  457. dst[1]=(char)(z>>8);
  458. dst[2]=(char)(z);
  459. #elif __BYTE_ORDER == __BIG_ENDIAN
  460. dst[0]=(char)(z);
  461. dst[1]=(char)(z>>8);
  462. dst[2]=(char)(z>>16);
  463. #endif
  464. dst += dst_skip;
  465. src++;
  466. }
  467. }
  468. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  469. {
  470. #if defined (__SSE2__) && !defined (__sun__)
  471. _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
  472. while (nsamples >= 4) {
  473. int i;
  474. int32_t z[4];
  475. __m128 samples = _mm_loadu_ps(src);
  476. __m128i converted = float_24_sse(samples);
  477. #ifdef __SSE4_1__
  478. z[0] = _mm_extract_epi32(converted, 0);
  479. z[1] = _mm_extract_epi32(converted, 1);
  480. z[2] = _mm_extract_epi32(converted, 2);
  481. z[3] = _mm_extract_epi32(converted, 3);
  482. #else
  483. __m128i shuffled1 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(0, 3, 2, 1));
  484. __m128i shuffled2 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(1, 0, 3, 2));
  485. __m128i shuffled3 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(2, 1, 0, 3));
  486. _mm_store_ss((float*)z, (__m128)converted);
  487. _mm_store_ss((float*)z+1, (__m128)shuffled1);
  488. _mm_store_ss((float*)z+2, (__m128)shuffled2);
  489. _mm_store_ss((float*)z+3, (__m128)shuffled3);
  490. #endif
  491. for (i = 0; i != 4; ++i) {
  492. memcpy (dst, z+i, 3);
  493. dst += dst_skip;
  494. }
  495. nsamples -= 4;
  496. src += 4;
  497. }
  498. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  499. unsigned long unrolled = nsamples / 4;
  500. while (unrolled--) {
  501. int i;
  502. int32_t z[4];
  503. float32x4_t samples = vld1q_f32(src);
  504. int32x4_t converted = float_24_neon(samples);
  505. vst1q_s32(z, converted);
  506. for (i = 0; i != 4; ++i) {
  507. memcpy (dst, z+i, 3);
  508. dst += dst_skip;
  509. }
  510. src += 4;
  511. }
  512. nsamples = nsamples & 3;
  513. #endif
  514. int32_t z;
  515. while (nsamples--) {
  516. float_24 (*src, z);
  517. #if __BYTE_ORDER == __LITTLE_ENDIAN
  518. memcpy (dst, &z, 3);
  519. #elif __BYTE_ORDER == __BIG_ENDIAN
  520. memcpy (dst, (char *)&z + 1, 3);
  521. #endif
  522. dst += dst_skip;
  523. src++;
  524. }
  525. }
  526. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  527. {
  528. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  529. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  530. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  531. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  532. int32_t x[4];
  533. memset(x, 0, sizeof(x));
  534. unsigned long unrolled = nsamples / 4;
  535. while (unrolled--) {
  536. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  537. // right aligned / inverse sequence below -> *256
  538. memcpy(((char*)&x[0])+1, src, 3);
  539. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  540. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  541. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  542. #else
  543. memcpy(&x[0], src, 3);
  544. memcpy(&x[1], src+src_skip, 3);
  545. memcpy(&x[2], src+2*src_skip, 3);
  546. memcpy(&x[3], src+3*src_skip, 3);
  547. #endif
  548. src += 4 * src_skip;
  549. int32x4_t source = vld1q_s32(x);
  550. source = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(source)));
  551. float32x4_t converted = vcvtq_f32_s32(source);
  552. float32x4_t scaled = vmulq_f32(converted, vscaling);
  553. vst1q_f32(dst, scaled);
  554. dst += 4;
  555. }
  556. nsamples = nsamples & 3;
  557. #endif
  558. /* ALERT: signed sign-extension portability !!! */
  559. while (nsamples--) {
  560. int x;
  561. #if __BYTE_ORDER == __LITTLE_ENDIAN
  562. x = (unsigned char)(src[0]);
  563. x <<= 8;
  564. x |= (unsigned char)(src[1]);
  565. x <<= 8;
  566. x |= (unsigned char)(src[2]);
  567. /* correct sign bit and the rest of the top byte */
  568. if (src[0] & 0x80) {
  569. x |= 0xff << 24;
  570. }
  571. #elif __BYTE_ORDER == __BIG_ENDIAN
  572. x = (unsigned char)(src[2]);
  573. x <<= 8;
  574. x |= (unsigned char)(src[1]);
  575. x <<= 8;
  576. x |= (unsigned char)(src[0]);
  577. /* correct sign bit and the rest of the top byte */
  578. if (src[2] & 0x80) {
  579. x |= 0xff << 24;
  580. }
  581. #endif
  582. *dst = x * scaling;
  583. dst++;
  584. src += src_skip;
  585. }
  586. }
  587. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  588. {
  589. const jack_default_audio_sample_t scaling = 1.f/SAMPLE_24BIT_SCALING;
  590. #if defined (__SSE2__) && !defined (__sun__)
  591. const __m128 scaling_block = _mm_set_ps1(scaling);
  592. while (nsamples >= 4) {
  593. int x0, x1, x2, x3;
  594. memcpy((char*)&x0 + 1, src, 3);
  595. memcpy((char*)&x1 + 1, src+src_skip, 3);
  596. memcpy((char*)&x2 + 1, src+2*src_skip, 3);
  597. memcpy((char*)&x3 + 1, src+3*src_skip, 3);
  598. src += 4 * src_skip;
  599. const __m128i block_i = _mm_set_epi32(x3, x2, x1, x0);
  600. const __m128i shifted = _mm_srai_epi32(block_i, 8);
  601. const __m128 converted = _mm_cvtepi32_ps (shifted);
  602. const __m128 scaled = _mm_mul_ps(converted, scaling_block);
  603. _mm_storeu_ps(dst, scaled);
  604. dst += 4;
  605. nsamples -= 4;
  606. }
  607. #elif defined (__ARM_NEON__) || defined (__ARM_NEON)
  608. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  609. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  610. int32_t x[4];
  611. memset(x, 0, sizeof(x));
  612. unsigned long unrolled = nsamples / 4;
  613. while (unrolled--) {
  614. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  615. // left aligned -> *256
  616. memcpy(&x[0], src, 3);
  617. memcpy(&x[1], src+src_skip, 3);
  618. memcpy(&x[2], src+2*src_skip, 3);
  619. memcpy(&x[3], src+3*src_skip, 3);
  620. #else
  621. memcpy(((char*)&x[0])+1, src, 3);
  622. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  623. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  624. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  625. #endif
  626. src += 4 * src_skip;
  627. int32x4_t source = vld1q_s32(x);
  628. float32x4_t converted = vcvtq_f32_s32(source);
  629. float32x4_t scaled = vmulq_f32(converted, vscaling);
  630. vst1q_f32(dst, scaled);
  631. dst += 4;
  632. }
  633. nsamples = nsamples & 3;
  634. #endif
  635. while (nsamples--) {
  636. int x;
  637. #if __BYTE_ORDER == __LITTLE_ENDIAN
  638. memcpy((char*)&x + 1, src, 3);
  639. #elif __BYTE_ORDER == __BIG_ENDIAN
  640. memcpy(&x, src, 3);
  641. #endif
  642. x >>= 8;
  643. *dst = x * scaling;
  644. dst++;
  645. src += src_skip;
  646. }
  647. }
  648. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  649. {
  650. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  651. unsigned long unrolled = nsamples / 4;
  652. nsamples = nsamples & 3;
  653. while (unrolled--) {
  654. float32x4_t samples = vld1q_f32(src);
  655. int16x4_t converted = float_16_neon(samples);
  656. converted = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(converted)));
  657. switch(dst_skip) {
  658. case 2:
  659. vst1_s16((int16_t*)dst, converted);
  660. break;
  661. default:
  662. vst1_lane_s16((int16_t*)(dst), converted, 0);
  663. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  664. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  665. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  666. break;
  667. }
  668. dst += 4*dst_skip;
  669. src+= 4;
  670. }
  671. #endif
  672. int16_t tmp;
  673. while (nsamples--) {
  674. // float_16 (*src, tmp);
  675. if (*src <= NORMALIZED_FLOAT_MIN) {
  676. tmp = SAMPLE_16BIT_MIN;
  677. } else if (*src >= NORMALIZED_FLOAT_MAX) {
  678. tmp = SAMPLE_16BIT_MAX;
  679. } else {
  680. tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
  681. }
  682. #if __BYTE_ORDER == __LITTLE_ENDIAN
  683. dst[0]=(char)(tmp>>8);
  684. dst[1]=(char)(tmp);
  685. #elif __BYTE_ORDER == __BIG_ENDIAN
  686. dst[0]=(char)(tmp);
  687. dst[1]=(char)(tmp>>8);
  688. #endif
  689. dst += dst_skip;
  690. src++;
  691. }
  692. }
  693. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  694. {
  695. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  696. unsigned long unrolled = nsamples / 4;
  697. nsamples = nsamples & 3;
  698. while (unrolled--) {
  699. float32x4_t samples = vld1q_f32(src);
  700. int16x4_t converted = float_16_neon(samples);
  701. switch(dst_skip) {
  702. case 2:
  703. vst1_s16((int16_t*)dst, converted);
  704. break;
  705. default:
  706. vst1_lane_s16((int16_t*)(dst), converted, 0);
  707. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  708. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  709. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  710. break;
  711. }
  712. dst += 4*dst_skip;
  713. src+= 4;
  714. }
  715. #endif
  716. while (nsamples--) {
  717. float_16 (*src, *((int16_t*) dst));
  718. dst += dst_skip;
  719. src++;
  720. }
  721. }
  722. 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)
  723. {
  724. jack_default_audio_sample_t val;
  725. int16_t tmp;
  726. while (nsamples--) {
  727. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float) UINT_MAX - 0.5f;
  728. float_16_scaled (val, tmp);
  729. #if __BYTE_ORDER == __LITTLE_ENDIAN
  730. dst[0]=(char)(tmp>>8);
  731. dst[1]=(char)(tmp);
  732. #elif __BYTE_ORDER == __BIG_ENDIAN
  733. dst[0]=(char)(tmp);
  734. dst[1]=(char)(tmp>>8);
  735. #endif
  736. dst += dst_skip;
  737. src++;
  738. }
  739. }
  740. 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)
  741. {
  742. jack_default_audio_sample_t val;
  743. while (nsamples--) {
  744. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float)UINT_MAX - 0.5f;
  745. float_16_scaled (val, *((int16_t*) dst));
  746. dst += dst_skip;
  747. src++;
  748. }
  749. }
  750. 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)
  751. {
  752. jack_default_audio_sample_t val;
  753. int16_t tmp;
  754. while (nsamples--) {
  755. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  756. float_16_scaled (val, tmp);
  757. #if __BYTE_ORDER == __LITTLE_ENDIAN
  758. dst[0]=(char)(tmp>>8);
  759. dst[1]=(char)(tmp);
  760. #elif __BYTE_ORDER == __BIG_ENDIAN
  761. dst[0]=(char)(tmp);
  762. dst[1]=(char)(tmp>>8);
  763. #endif
  764. dst += dst_skip;
  765. src++;
  766. }
  767. }
  768. 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)
  769. {
  770. jack_default_audio_sample_t val;
  771. while (nsamples--) {
  772. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  773. float_16_scaled (val, *((int16_t*) dst));
  774. dst += dst_skip;
  775. src++;
  776. }
  777. }
  778. 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)
  779. {
  780. jack_default_audio_sample_t x;
  781. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  782. jack_default_audio_sample_t xp; /* x' */
  783. float r;
  784. float rm1 = state->rm1;
  785. unsigned int idx = state->idx;
  786. int16_t tmp;
  787. while (nsamples--) {
  788. x = *src * SAMPLE_16BIT_SCALING;
  789. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  790. /* Filter the error with Lipshitz's minimally audible FIR:
  791. [2.033 -2.165 1.959 -1.590 0.6149] */
  792. xe = x
  793. - state->e[idx] * 2.033f
  794. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  795. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  796. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  797. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  798. xp = xe + r - rm1;
  799. rm1 = r;
  800. float_16_scaled (xp, tmp);
  801. /* Intrinsic z^-1 delay */
  802. idx = (idx + 1) & DITHER_BUF_MASK;
  803. state->e[idx] = xp - xe;
  804. #if __BYTE_ORDER == __LITTLE_ENDIAN
  805. dst[0]=(char)(tmp>>8);
  806. dst[1]=(char)(tmp);
  807. #elif __BYTE_ORDER == __BIG_ENDIAN
  808. dst[0]=(char)(tmp);
  809. dst[1]=(char)(tmp>>8);
  810. #endif
  811. dst += dst_skip;
  812. src++;
  813. }
  814. state->rm1 = rm1;
  815. state->idx = idx;
  816. }
  817. 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)
  818. {
  819. jack_default_audio_sample_t x;
  820. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  821. jack_default_audio_sample_t xp; /* x' */
  822. float r;
  823. float rm1 = state->rm1;
  824. unsigned int idx = state->idx;
  825. while (nsamples--) {
  826. x = *src * SAMPLE_16BIT_SCALING;
  827. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  828. /* Filter the error with Lipshitz's minimally audible FIR:
  829. [2.033 -2.165 1.959 -1.590 0.6149] */
  830. xe = x
  831. - state->e[idx] * 2.033f
  832. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  833. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  834. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  835. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  836. xp = xe + r - rm1;
  837. rm1 = r;
  838. float_16_scaled (xp, *((int16_t*) dst));
  839. /* Intrinsic z^-1 delay */
  840. idx = (idx + 1) & DITHER_BUF_MASK;
  841. state->e[idx] = *((int16_t*) dst) - xe;
  842. dst += dst_skip;
  843. src++;
  844. }
  845. state->rm1 = rm1;
  846. state->idx = idx;
  847. }
  848. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  849. {
  850. short z;
  851. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  852. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  853. const float32x4_t vscaling = vdupq_n_f32(scaling);
  854. unsigned long unrolled = nsamples / 4;
  855. while (unrolled--) {
  856. int16x4_t source16x4;
  857. switch(src_skip) {
  858. case 2:
  859. source16x4 = vld1_s16((int16_t*)src);
  860. break;
  861. case 4:
  862. source16x4 = vld2_s16((int16_t*)src).val[0];
  863. break;
  864. default:
  865. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  866. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  867. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  868. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  869. break;
  870. }
  871. source16x4 = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(source16x4)));
  872. int32x4_t source32x4 = vmovl_s16(source16x4);
  873. src += 4 * src_skip;
  874. float32x4_t converted = vcvtq_f32_s32(source32x4);
  875. float32x4_t scaled = vmulq_f32(converted, vscaling);
  876. vst1q_f32(dst, scaled);
  877. dst += 4;
  878. }
  879. nsamples = nsamples & 3;
  880. #endif
  881. /* ALERT: signed sign-extension portability !!! */
  882. while (nsamples--) {
  883. #if __BYTE_ORDER == __LITTLE_ENDIAN
  884. z = (unsigned char)(src[0]);
  885. z <<= 8;
  886. z |= (unsigned char)(src[1]);
  887. #elif __BYTE_ORDER == __BIG_ENDIAN
  888. z = (unsigned char)(src[1]);
  889. z <<= 8;
  890. z |= (unsigned char)(src[0]);
  891. #endif
  892. *dst = z * scaling;
  893. dst++;
  894. src += src_skip;
  895. }
  896. }
  897. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  898. {
  899. /* ALERT: signed sign-extension portability !!! */
  900. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  901. #if defined (__ARM_NEON__) || defined (__ARM_NEON)
  902. const float32x4_t vscaling = vdupq_n_f32(scaling);
  903. unsigned long unrolled = nsamples / 4;
  904. while (unrolled--) {
  905. int16x4_t source16x4;
  906. switch(src_skip) {
  907. case 2:
  908. source16x4 = vld1_s16((int16_t*)src);
  909. break;
  910. case 4:
  911. source16x4 = vld2_s16((int16_t*)src).val[0];
  912. break;
  913. default:
  914. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  915. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  916. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  917. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  918. break;
  919. }
  920. int32x4_t source32x4 = vmovl_s16(source16x4);
  921. src += 4 * src_skip;
  922. float32x4_t converted = vcvtq_f32_s32(source32x4);
  923. float32x4_t scaled = vmulq_f32(converted, vscaling);
  924. vst1q_f32(dst, scaled);
  925. dst += 4;
  926. }
  927. nsamples = nsamples & 3;
  928. #endif
  929. while (nsamples--) {
  930. *dst = (*((short *) src)) * scaling;
  931. dst++;
  932. src += src_skip;
  933. }
  934. }
  935. void memset_interleave (char *dst, char val, unsigned long bytes,
  936. unsigned long unit_bytes,
  937. unsigned long skip_bytes)
  938. {
  939. switch (unit_bytes) {
  940. case 1:
  941. while (bytes--) {
  942. *dst = val;
  943. dst += skip_bytes;
  944. }
  945. break;
  946. case 2:
  947. while (bytes) {
  948. *((short *) dst) = (short) val;
  949. dst += skip_bytes;
  950. bytes -= 2;
  951. }
  952. break;
  953. case 4:
  954. while (bytes) {
  955. *((int *) dst) = (int) val;
  956. dst += skip_bytes;
  957. bytes -= 4;
  958. }
  959. break;
  960. default:
  961. while (bytes) {
  962. memset(dst, val, unit_bytes);
  963. dst += skip_bytes;
  964. bytes -= unit_bytes;
  965. }
  966. break;
  967. }
  968. }
  969. /* COPY FUNCTIONS: used to move data from an input channel to an
  970. output channel. Note that we assume that the skip distance
  971. is the same for both channels. This is completely fine
  972. unless the input and output were on different audio interfaces that
  973. were interleaved differently. We don't try to handle that.
  974. */
  975. void
  976. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  977. {
  978. memcpy (dst, src, src_bytes);
  979. }
  980. void
  981. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  982. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  983. {
  984. while (src_bytes) {
  985. *((short *) dst) = *((short *) src);
  986. dst += dst_skip_bytes;
  987. src += src_skip_bytes;
  988. src_bytes -= 2;
  989. }
  990. }
  991. void
  992. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  993. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  994. {
  995. while (src_bytes) {
  996. memcpy(dst, src, 3);
  997. dst += dst_skip_bytes;
  998. src += src_skip_bytes;
  999. src_bytes -= 3;
  1000. }
  1001. }
  1002. void
  1003. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1004. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1005. {
  1006. while (src_bytes) {
  1007. *((int *) dst) = *((int *) src);
  1008. dst += dst_skip_bytes;
  1009. src += src_skip_bytes;
  1010. src_bytes -= 4;
  1011. }
  1012. }