jack1 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.

453 lines
11KB

  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. $Id$
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <memory.h>
  20. #include <stdlib.h>
  21. #include <limits.h>
  22. #include <jack/memops.h>
  23. #define SAMPLE_MAX_24BIT 8388607.0f
  24. #define SAMPLE_MAX_16BIT 32767.0f
  25. /* Round float to int (can't go in memops.h otherwise it gets multiply
  26. defined) */
  27. inline int f_round(float f) {
  28. f += (3<<22);
  29. return *((int*)&f) - 0x4b400000;
  30. }
  31. /* XXX we could use rint(), but for now we'll accept whatever default
  32. floating-point => int conversion the compiler provides.
  33. */
  34. void sample_move_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  35. {
  36. /* ALERT: signed sign-extension portability !!! */
  37. while (nsamples--) {
  38. *((int *) dst) = ((int) (*src * SAMPLE_MAX_24BIT)) << 8;
  39. dst += dst_skip;
  40. src++;
  41. }
  42. }
  43. void sample_move_dS_s32u24 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  44. {
  45. /* ALERT: signed sign-extension portability !!! */
  46. while (nsamples--) {
  47. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  48. dst++;
  49. src += src_skip;
  50. }
  51. }
  52. void sample_move_dither_rect_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  53. {
  54. /* ALERT: signed sign-extension portability !!! */
  55. sample_t x;
  56. long long y;
  57. while (nsamples--) {
  58. x = *src * SAMPLE_MAX_16BIT;
  59. x -= (float)rand() / (float)RAND_MAX;
  60. y = (long long)f_round(x);
  61. y <<= 16;
  62. if (y > INT_MAX) {
  63. *((int *) dst) = INT_MAX;
  64. } else if (y < INT_MIN) {
  65. *((int *) dst) = INT_MIN;
  66. } else {
  67. *((int *) dst) = (int)y;
  68. }
  69. dst += dst_skip;
  70. src++;
  71. }
  72. }
  73. void sample_move_dither_tri_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  74. {
  75. sample_t x;
  76. float r;
  77. float rm1 = state->rm1;
  78. long long y;
  79. while (nsamples--) {
  80. x = *src * (float)SAMPLE_MAX_16BIT;
  81. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  82. x += r - rm1;
  83. rm1 = r;
  84. /* swh: This could be some inline asm on x86 */
  85. y = (long long)f_round(x);
  86. y <<= 16;
  87. if (y > INT_MAX) {
  88. *((int *) dst) = INT_MAX;
  89. } else if (y < INT_MIN) {
  90. *((int *) dst) = INT_MIN;
  91. } else {
  92. *((int *) dst) = (int)y;
  93. }
  94. dst += dst_skip;
  95. src++;
  96. }
  97. state->rm1 = rm1;
  98. }
  99. void sample_move_dither_shaped_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  100. {
  101. sample_t x;
  102. sample_t xe; /* the innput sample - filtered error */
  103. sample_t xp; /* x' */
  104. float r;
  105. float rm1 = state->rm1;
  106. unsigned int idx = state->idx;
  107. long long y;
  108. while (nsamples--) {
  109. x = *src * (float)SAMPLE_MAX_16BIT;
  110. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  111. /* Filter the error with Lipshitz's minimally audible FIR:
  112. [2.033 -2.165 1.959 -1.590 0.6149] */
  113. xe = x
  114. - state->e[idx] * 2.033f
  115. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  116. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  117. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  118. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  119. xp = xe + r - rm1;
  120. rm1 = r;
  121. /* This could be some inline asm on x86 */
  122. y = (long long)f_round(xp);
  123. /* Intrinsic z^-1 delay */
  124. idx = (idx + 1) & DITHER_BUF_MASK;
  125. state->e[idx] = y - xe;
  126. y <<= 16;
  127. if (y > INT_MAX) {
  128. *((int *) dst) = INT_MAX;
  129. } else if (y < INT_MIN) {
  130. *((int *) dst) = INT_MIN;
  131. } else {
  132. *((int *) dst) = (int)y;
  133. }
  134. dst += dst_skip;
  135. src++;
  136. }
  137. state->rm1 = rm1;
  138. state->idx = idx;
  139. }
  140. void sample_move_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  141. {
  142. sample_t val;
  143. /* ALERT: signed sign-extension portability !!! */
  144. /* XXX good to use x86 assembler here, since float->short
  145. sucks that h/w.
  146. */
  147. while (nsamples--) {
  148. val = *src;
  149. if (val > 1.0f) {
  150. *((short *)dst) = SHRT_MAX;
  151. } else if (val < -1.0f) {
  152. *((short *)dst) = SHRT_MIN;
  153. } else {
  154. *((short *) dst) = (short) (val * SAMPLE_MAX_16BIT);
  155. }
  156. dst += dst_skip;
  157. src++;
  158. }
  159. }
  160. void sample_move_dither_rect_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  161. {
  162. sample_t val;
  163. int tmp;
  164. while (nsamples--) {
  165. val = *src * (float)SAMPLE_MAX_16BIT;
  166. val -= (float)rand() / (float)RAND_MAX;
  167. /* swh: This could be some inline asm on x86 */
  168. tmp = f_round(val);
  169. if (tmp > SHRT_MAX) {
  170. *((short *)dst) = SHRT_MAX;
  171. } else if (tmp < SHRT_MIN) {
  172. *((short *)dst) = SHRT_MIN;
  173. } else {
  174. *((short *) dst) = (short)tmp;
  175. }
  176. dst += dst_skip;
  177. src++;
  178. }
  179. }
  180. void sample_move_dither_tri_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  181. {
  182. sample_t x;
  183. float r;
  184. float rm1 = state->rm1;
  185. int y;
  186. while (nsamples--) {
  187. x = *src * (float)SAMPLE_MAX_16BIT;
  188. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  189. x += r - rm1;
  190. rm1 = r;
  191. /* swh: This could be some inline asm on x86 */
  192. y = f_round(x);
  193. if (y > SHRT_MAX) {
  194. *((short *)dst) = SHRT_MAX;
  195. } else if (y < SHRT_MIN) {
  196. *((short *)dst) = SHRT_MIN;
  197. } else {
  198. *((short *) dst) = (short)y;
  199. }
  200. dst += dst_skip;
  201. src++;
  202. }
  203. state->rm1 = rm1;
  204. }
  205. void sample_move_dither_shaped_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  206. {
  207. sample_t x;
  208. sample_t xe; /* the innput sample - filtered error */
  209. sample_t xp; /* x' */
  210. float r;
  211. float rm1 = state->rm1;
  212. unsigned int idx = state->idx;
  213. int y;
  214. while (nsamples--) {
  215. x = *src * (float)SAMPLE_MAX_16BIT;
  216. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  217. /* Filter the error with Lipshitz's minimally audible FIR:
  218. [2.033 -2.165 1.959 -1.590 0.6149] */
  219. xe = x
  220. - state->e[idx] * 2.033f
  221. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  222. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  223. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  224. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  225. xp = xe + r - rm1;
  226. rm1 = r;
  227. /* This could be some inline asm on x86 */
  228. y = f_round(xp);
  229. /* Intrinsic z^-1 delay */
  230. idx = (idx + 1) & DITHER_BUF_MASK;
  231. state->e[idx] = y - xe;
  232. if (y > SHRT_MAX) {
  233. *((short *)dst) = SHRT_MAX;
  234. } else if (y < SHRT_MIN) {
  235. *((short *)dst) = SHRT_MIN;
  236. } else {
  237. *((short *) dst) = (short)y;
  238. }
  239. dst += dst_skip;
  240. src++;
  241. }
  242. state->rm1 = rm1;
  243. state->idx = idx;
  244. }
  245. void sample_move_dS_s16 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  246. {
  247. /* ALERT: signed sign-extension portability !!! */
  248. while (nsamples--) {
  249. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  250. dst++;
  251. src += src_skip;
  252. }
  253. }
  254. void sample_merge_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  255. {
  256. short val;
  257. /* ALERT: signed sign-extension portability !!! */
  258. while (nsamples--) {
  259. val = (short) (*src * SAMPLE_MAX_16BIT);
  260. if (val > SHRT_MAX - *((short *) dst)) {
  261. *((short *)dst) = SHRT_MAX;
  262. } else if (val < SHRT_MIN - *((short *) dst)) {
  263. *((short *)dst) = SHRT_MIN;
  264. } else {
  265. *((short *) dst) += val;
  266. }
  267. dst += dst_skip;
  268. src++;
  269. }
  270. }
  271. void sample_merge_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  272. {
  273. /* ALERT: signed sign-extension portability !!! */
  274. while (nsamples--) {
  275. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  276. dst += dst_skip;
  277. src++;
  278. }
  279. }
  280. void memset_interleave (char *dst, char val, unsigned long bytes,
  281. unsigned long unit_bytes,
  282. unsigned long skip_bytes)
  283. {
  284. switch (unit_bytes) {
  285. case 1:
  286. while (bytes--) {
  287. *dst = val;
  288. dst += skip_bytes;
  289. }
  290. break;
  291. case 2:
  292. while (bytes) {
  293. *((short *) dst) = (short) val;
  294. dst += skip_bytes;
  295. bytes -= 2;
  296. }
  297. break;
  298. case 4:
  299. while (bytes) {
  300. *((int *) dst) = (int) val;
  301. dst += skip_bytes;
  302. bytes -= 4;
  303. }
  304. break;
  305. }
  306. }
  307. /* COPY FUNCTIONS: used to move data from an input channel to an
  308. output channel. Note that we assume that the skip distance
  309. is the same for both channels. This is completely fine
  310. unless the input and output were on different audio interfaces that
  311. were interleaved differently. We don't try to handle that.
  312. */
  313. void
  314. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  315. {
  316. memcpy (dst, src, src_bytes);
  317. }
  318. void
  319. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  320. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  321. {
  322. while (src_bytes) {
  323. *((short *) dst) += *((short *) src);
  324. dst += 2;
  325. src += 2;
  326. src_bytes -= 2;
  327. }
  328. }
  329. void
  330. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  331. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  332. {
  333. while (src_bytes) {
  334. *((int *) dst) += *((int *) src);
  335. dst += 4;
  336. src += 4;
  337. src_bytes -= 4;
  338. }
  339. }
  340. void
  341. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  342. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  343. {
  344. while (src_bytes) {
  345. *((short *) dst) += *((short *) src);
  346. dst += dst_skip_bytes;
  347. src += src_skip_bytes;
  348. src_bytes -= 2;
  349. }
  350. }
  351. void
  352. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  353. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  354. {
  355. while (src_bytes) {
  356. *((int *) dst) += *((int *) src);
  357. dst += dst_skip_bytes;
  358. src += src_skip_bytes;
  359. src_bytes -= 4;
  360. }
  361. }
  362. void
  363. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  364. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  365. {
  366. while (src_bytes) {
  367. *((short *) dst) = *((short *) src);
  368. dst += dst_skip_bytes;
  369. src += src_skip_bytes;
  370. src_bytes -= 2;
  371. }
  372. }
  373. void
  374. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  375. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  376. {
  377. while (src_bytes) {
  378. *((int *) dst) = *((int *) src);
  379. dst += dst_skip_bytes;
  380. src += src_skip_bytes;
  381. src_bytes -= 4;
  382. }
  383. }