output_pack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. // Scale and pack functions
  22. #include "squeezelite.h"
  23. #define MAX_SCALESAMPLE 0x7fffffffffffLL
  24. #define MIN_SCALESAMPLE -MAX_SCALESAMPLE
  25. // inlining these on windows prevents them being linkable...
  26. #if !WIN
  27. inline
  28. #endif
  29. s32_t gain(s32_t gain, s32_t sample) {
  30. s64_t res = (s64_t)gain * (s64_t)sample;
  31. if (res > MAX_SCALESAMPLE) res = MAX_SCALESAMPLE;
  32. if (res < MIN_SCALESAMPLE) res = MIN_SCALESAMPLE;
  33. return (s32_t) (res >> 16);
  34. }
  35. #if !WIN
  36. inline
  37. #endif
  38. s32_t to_gain(float f) {
  39. return (s32_t)(f * 65536.0F);
  40. }
  41. void _scale_and_pack_frames(void *outputptr, s32_t *inputptr, frames_t cnt, s32_t gainL, s32_t gainR, u8_t flags, output_format format) {
  42. // in-place copy input samples if mono/combined is used (never happens with DSD active)
  43. if ((flags & MONO_LEFT) && (flags & MONO_RIGHT)) {
  44. s32_t *ptr = inputptr;
  45. frames_t count = cnt;
  46. while (count--) {
  47. // use 64 bits integer for purists but should really not care
  48. *ptr = *(ptr + 1) = ((s64_t) *ptr + (s64_t) *(ptr + 1)) / 2;
  49. ptr += 2;
  50. }
  51. } else if (flags & MONO_RIGHT) {
  52. s32_t *ptr = inputptr + 1;
  53. frames_t count = cnt;
  54. while (count--) {
  55. *(ptr - 1) = *ptr;
  56. ptr += 2;
  57. }
  58. } else if (flags & MONO_LEFT) {
  59. s32_t *ptr = inputptr;
  60. frames_t count = cnt;
  61. while (count--) {
  62. *(ptr + 1) = *ptr;
  63. ptr += 2;
  64. }
  65. }
  66. switch(format) {
  67. #if DSD
  68. case U32_LE:
  69. {
  70. #if SL_LITTLE_ENDIAN
  71. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  72. #else
  73. u32_t *optr = (u32_t *)(void *)outputptr;
  74. while (cnt--) {
  75. s32_t lsample = *(inputptr++);
  76. s32_t rsample = *(inputptr++);
  77. *(optr++) =
  78. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  79. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  80. *(optr++) =
  81. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  82. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  83. }
  84. #endif
  85. }
  86. break;
  87. case U32_BE:
  88. {
  89. #if SL_LITTLE_ENDIAN
  90. u32_t *optr = (u32_t *)(void *)outputptr;
  91. while (cnt--) {
  92. s32_t lsample = *(inputptr++);
  93. s32_t rsample = *(inputptr++);
  94. *(optr++) =
  95. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  96. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  97. *(optr++) =
  98. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  99. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  100. }
  101. #else
  102. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  103. #endif
  104. }
  105. break;
  106. case U16_LE:
  107. {
  108. u32_t *optr = (u32_t *)(void *)outputptr;
  109. #if SL_LITTLE_ENDIAN
  110. while (cnt--) {
  111. *(optr++) = (*(inputptr) >> 16 & 0x0000ffff) | (*(inputptr + 1) & 0xffff0000);
  112. inputptr += 2;
  113. }
  114. #else
  115. while (cnt--) {
  116. s32_t lsample = *(inputptr++);
  117. s32_t rsample = *(inputptr++);
  118. *(optr++) =
  119. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  120. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  121. }
  122. #endif
  123. }
  124. break;
  125. case U16_BE:
  126. {
  127. u32_t *optr = (u32_t *)(void *)outputptr;
  128. #if SL_LITTLE_ENDIAN
  129. while (cnt--) {
  130. s32_t lsample = *(inputptr++);
  131. s32_t rsample = *(inputptr++);
  132. *(optr++) =
  133. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  134. (rsample & 0xff000000) >> 8 | (rsample & 0x00ff0000) << 8;
  135. }
  136. #else
  137. while (cnt--) {
  138. *(optr++) = (*(inputptr) & 0xffff0000) | (*(inputptr + 1) >> 16 & 0x0000ffff);
  139. inputptr += 2;
  140. }
  141. #endif
  142. }
  143. break;
  144. case U8:
  145. {
  146. u16_t *optr = (u16_t *)(void *)outputptr;
  147. #if SL_LITTLE_ENDIAN
  148. while (cnt--) {
  149. *(optr++) = (u16_t)((*(inputptr) >> 24 & 0x000000ff) | (*(inputptr + 1) >> 16 & 0x0000ff00));
  150. inputptr += 2;
  151. }
  152. #else
  153. while (cnt--) {
  154. *(optr++) = (u16_t)((*(inputptr) >> 16 & 0x0000ff00) | (*(inputptr + 1) >> 24 & 0x000000ff));
  155. inputptr += 2;
  156. }
  157. #endif
  158. }
  159. break;
  160. #endif
  161. case S16_LE:
  162. {
  163. u32_t *optr = (u32_t *)(void *)outputptr;
  164. #if SL_LITTLE_ENDIAN
  165. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  166. while (cnt--) {
  167. *(optr++) = (*(inputptr) >> 16 & 0x0000ffff) | (*(inputptr + 1) & 0xffff0000);
  168. inputptr += 2;
  169. }
  170. } else {
  171. while (cnt--) {
  172. *(optr++) = (gain(gainL, *(inputptr)) >> 16 & 0x0000ffff) | (gain(gainR, *(inputptr+1)) & 0xffff0000);
  173. inputptr += 2;
  174. }
  175. }
  176. #else
  177. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  178. while (cnt--) {
  179. s32_t lsample = *(inputptr++);
  180. s32_t rsample = *(inputptr++);
  181. *(optr++) =
  182. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  183. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  184. }
  185. } else {
  186. while (cnt--) {
  187. s32_t lsample = gain(gainL, *(inputptr++));
  188. s32_t rsample = gain(gainR, *(inputptr++));
  189. *(optr++) =
  190. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  191. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  192. }
  193. }
  194. #endif
  195. }
  196. break;
  197. case S24_LE:
  198. {
  199. u32_t *optr = (u32_t *)(void *)outputptr;
  200. #if SL_LITTLE_ENDIAN
  201. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  202. while (cnt--) {
  203. *(optr++) = *(inputptr++) >> 8;
  204. *(optr++) = *(inputptr++) >> 8;
  205. }
  206. } else {
  207. while (cnt--) {
  208. *(optr++) = gain(gainL, *(inputptr++)) >> 8;
  209. *(optr++) = gain(gainR, *(inputptr++)) >> 8;
  210. }
  211. }
  212. #else
  213. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  214. while (cnt--) {
  215. s32_t lsample = *(inputptr++);
  216. s32_t rsample = *(inputptr++);
  217. *(optr++) =
  218. (lsample & 0xff000000) >> 16 | (lsample & 0x00ff0000) | (lsample & 0x0000ff00 << 16);
  219. *(optr++) =
  220. (rsample & 0xff000000) >> 16 | (rsample & 0x00ff0000) | (rsample & 0x0000ff00 << 16);
  221. }
  222. } else {
  223. while (cnt--) {
  224. s32_t lsample = gain(gainL, *(inputptr++));
  225. s32_t rsample = gain(gainR, *(inputptr++));
  226. *(optr++) =
  227. (lsample & 0xff000000) >> 16 | (lsample & 0x00ff0000) | (lsample & 0x0000ff00 << 16);
  228. *(optr++) =
  229. (rsample & 0xff000000) >> 16 | (rsample & 0x00ff0000) | (rsample & 0x0000ff00 << 16);
  230. }
  231. }
  232. #endif
  233. }
  234. break;
  235. case S24_3LE:
  236. {
  237. u8_t *optr = (u8_t *)(void *)outputptr;
  238. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  239. while (cnt) {
  240. // attempt to do 32 bit memory accesses - move 2 frames at once: 16 bytes -> 12 bytes
  241. // falls through to exception case when not aligned or if less than 2 frames to move
  242. if (((uintptr_t)optr & 0x3) == 0 && cnt >= 2) {
  243. u32_t *o_ptr = (u32_t *)(void *)optr;
  244. while (cnt >= 2) {
  245. s32_t l1 = *(inputptr++); s32_t r1 = *(inputptr++);
  246. s32_t l2 = *(inputptr++); s32_t r2 = *(inputptr++);
  247. #if SL_LITTLE_ENDIAN
  248. *(o_ptr++) = (l1 & 0xffffff00) >> 8 | (r1 & 0x0000ff00) << 16;
  249. *(o_ptr++) = (r1 & 0xffff0000) >> 16 | (l2 & 0x00ffff00) << 8;
  250. *(o_ptr++) = (l2 & 0xff000000) >> 24 | (r2 & 0xffffff00);
  251. #else
  252. *(o_ptr++) = (l1 & 0x0000ff00) << 16 | (l1 & 0x00ff0000) | (l1 & 0xff000000) >> 16 |
  253. (r1 & 0x0000ff00) >> 8;
  254. *(o_ptr++) = (r1 & 0x00ff0000) << 8 | (r1 & 0xff000000) >> 8 | (l2 & 0x0000ff00) |
  255. (l2 & 0x00ff0000) >> 16;
  256. *(o_ptr++) = (l2 & 0xff000000) | (r2 & 0x0000ff00) << 8 | (r2 & 0x00ff0000) >> 8 |
  257. (r2 & 0xff000000) >> 24;
  258. #endif
  259. optr += 12;
  260. cnt -= 2;
  261. }
  262. } else {
  263. s32_t lsample = *(inputptr++);
  264. s32_t rsample = *(inputptr++);
  265. *(optr++) = (lsample & 0x0000ff00) >> 8;
  266. *(optr++) = (lsample & 0x00ff0000) >> 16;
  267. *(optr++) = (lsample & 0xff000000) >> 24;
  268. *(optr++) = (rsample & 0x0000ff00) >> 8;
  269. *(optr++) = (rsample & 0x00ff0000) >> 16;
  270. *(optr++) = (rsample & 0xff000000) >> 24;
  271. cnt--;
  272. }
  273. }
  274. } else {
  275. while (cnt) {
  276. // attempt to do 32 bit memory accesses - move 2 frames at once: 16 bytes -> 12 bytes
  277. // falls through to exception case when not aligned or if less than 2 frames to move
  278. if (((uintptr_t)optr & 0x3) == 0 && cnt >= 2) {
  279. u32_t *o_ptr = (u32_t *)(void *)optr;
  280. while (cnt >= 2) {
  281. s32_t l1 = gain(gainL, *(inputptr++)); s32_t r1 = gain(gainR, *(inputptr++));
  282. s32_t l2 = gain(gainL, *(inputptr++)); s32_t r2 = gain(gainR, *(inputptr++));
  283. #if SL_LITTLE_ENDIAN
  284. *(o_ptr++) = (l1 & 0xffffff00) >> 8 | (r1 & 0x0000ff00) << 16;
  285. *(o_ptr++) = (r1 & 0xffff0000) >> 16 | (l2 & 0x00ffff00) << 8;
  286. *(o_ptr++) = (l2 & 0xff000000) >> 24 | (r2 & 0xffffff00);
  287. #else
  288. *(o_ptr++) = (l1 & 0x0000ff00) << 16 | (l1 & 0x00ff0000) | (l1 & 0xff000000) >> 16 |
  289. (r1 & 0x0000ff00) >> 8;
  290. *(o_ptr++) = (r1 & 0x00ff0000) << 8 | (r1 & 0xff000000) >> 8 | (l2 & 0x0000ff00) |
  291. (l2 & 0x00ff0000) >> 16;
  292. *(o_ptr++) = (l2 & 0xff000000) | (r2 & 0x0000ff00) << 8 | (r2 & 0x00ff0000) >> 8 |
  293. (r2 & 0xff000000) >> 24;
  294. #endif
  295. optr += 12;
  296. cnt -= 2;
  297. }
  298. } else {
  299. s32_t lsample = gain(gainL, *(inputptr++));
  300. s32_t rsample = gain(gainR, *(inputptr++));
  301. *(optr++) = (lsample & 0x0000ff00) >> 8;
  302. *(optr++) = (lsample & 0x00ff0000) >> 16;
  303. *(optr++) = (lsample & 0xff000000) >> 24;
  304. *(optr++) = (rsample & 0x0000ff00) >> 8;
  305. *(optr++) = (rsample & 0x00ff0000) >> 16;
  306. *(optr++) = (rsample & 0xff000000) >> 24;
  307. cnt--;
  308. }
  309. }
  310. }
  311. }
  312. break;
  313. case S32_LE:
  314. {
  315. u32_t *optr = (u32_t *)(void *)outputptr;
  316. #if SL_LITTLE_ENDIAN
  317. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  318. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  319. } else {
  320. while (cnt--) {
  321. *(optr++) = gain(gainL, *(inputptr++));
  322. *(optr++) = gain(gainR, *(inputptr++));
  323. }
  324. }
  325. #else
  326. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  327. while (cnt--) {
  328. s32_t lsample = *(inputptr++);
  329. s32_t rsample = *(inputptr++);
  330. *(optr++) =
  331. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  332. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  333. *(optr++) =
  334. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  335. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  336. }
  337. } else {
  338. while (cnt--) {
  339. s32_t lsample = gain(gainL, *(inputptr++));
  340. s32_t rsample = gain(gainR, *(inputptr++));
  341. *(optr++) =
  342. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  343. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  344. *(optr++) =
  345. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  346. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  347. }
  348. }
  349. #endif
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. #if !WIN
  357. inline
  358. #endif
  359. void _apply_cross(struct buffer *outputbuf, frames_t out_frames, s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  360. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp;
  361. frames_t count = out_frames * 2;
  362. while (count--) {
  363. if (*cross_ptr > (ISAMPLE_T *)outputbuf->wrap) {
  364. *cross_ptr -= outputbuf->size / BYTES_PER_FRAME * 2;
  365. }
  366. *ptr = gain(cross_gain_out, *ptr) + gain(cross_gain_in, **cross_ptr);
  367. ptr++; (*cross_ptr)++;
  368. }
  369. }
  370. #if !WIN
  371. inline
  372. #endif
  373. void _apply_gain(struct buffer *outputbuf, frames_t count, s32_t gainL, s32_t gainR, u8_t flags) {
  374. if (gainL == FIXED_ONE && gainR == FIXED_ONE && !(flags & (MONO_LEFT | MONO_RIGHT))) {
  375. return;
  376. } else if ((flags & MONO_LEFT) && (flags & MONO_RIGHT)) {
  377. ISAMPLE_T *ptrL = (ISAMPLE_T *)(void *)outputbuf->readp;
  378. ISAMPLE_T *ptrR = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  379. while (count--) {
  380. *ptrL = *ptrR = (gain(gainL, *ptrL) + gain(gainR, *ptrR)) / 2;
  381. ptrL += 2; ptrR += 2;
  382. }
  383. } else if (flags & MONO_RIGHT) {
  384. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  385. while (count--) {
  386. *(ptr - 1) = *ptr = gain(gainR, *ptr);
  387. ptr += 2;
  388. }
  389. } else if (flags & MONO_LEFT) {
  390. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp;
  391. while (count--) {
  392. *(ptr + 1) = *ptr = gain(gainL, *ptr);
  393. ptr += 2;
  394. }
  395. } else {
  396. ISAMPLE_T *ptrL = (ISAMPLE_T *)(void *)outputbuf->readp;
  397. ISAMPLE_T *ptrR = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  398. while (count--) {
  399. *ptrL = gain(gainL, *ptrL);
  400. *ptrR = gain(gainR, *ptrR);
  401. ptrL += 2; ptrR += 2;
  402. }
  403. }
  404. }