output_pack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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, output_format format) {
  42. // in-place copy input samples if mono/combined is used (never happens with DSD active)
  43. if ((gainR & MONO_FLAG) && (gainL & MONO_FLAG)) {
  44. s32_t *ptr = inputptr;
  45. frames_t count = cnt;
  46. gainL &= ~MONO_FLAG; gainR &= ~MONO_FLAG;
  47. while (count--) {
  48. // use 64 bits integer for purists but should really not care
  49. *ptr = *(ptr + 1) = ((s64_t) *ptr + (s64_t) *(ptr + 1)) / 2;
  50. ptr += 2;
  51. }
  52. } else if (gainL & MONO_FLAG) {
  53. s32_t *ptr = inputptr + 1;
  54. frames_t count = cnt;
  55. gainL &= ~MONO_FLAG;
  56. while (count--) {
  57. *(ptr - 1) = *ptr;
  58. ptr += 2;
  59. }
  60. } else if (gainR & MONO_FLAG) {
  61. s32_t *ptr = inputptr;
  62. frames_t count = cnt;
  63. gainR &= ~MONO_FLAG;
  64. while (count--) {
  65. *(ptr + 1) = *ptr;
  66. ptr += 2;
  67. }
  68. }
  69. switch(format) {
  70. #if DSD
  71. case U32_LE:
  72. {
  73. #if SL_LITTLE_ENDIAN
  74. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  75. #else
  76. u32_t *optr = (u32_t *)(void *)outputptr;
  77. while (cnt--) {
  78. s32_t lsample = *(inputptr++);
  79. s32_t rsample = *(inputptr++);
  80. *(optr++) =
  81. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  82. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  83. *(optr++) =
  84. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  85. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  86. }
  87. #endif
  88. }
  89. break;
  90. case U32_BE:
  91. {
  92. #if SL_LITTLE_ENDIAN
  93. u32_t *optr = (u32_t *)(void *)outputptr;
  94. while (cnt--) {
  95. s32_t lsample = *(inputptr++);
  96. s32_t rsample = *(inputptr++);
  97. *(optr++) =
  98. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  99. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  100. *(optr++) =
  101. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  102. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  103. }
  104. #else
  105. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  106. #endif
  107. }
  108. break;
  109. case U16_LE:
  110. {
  111. u32_t *optr = (u32_t *)(void *)outputptr;
  112. #if SL_LITTLE_ENDIAN
  113. while (cnt--) {
  114. *(optr++) = (*(inputptr) >> 16 & 0x0000ffff) | (*(inputptr + 1) & 0xffff0000);
  115. inputptr += 2;
  116. }
  117. #else
  118. while (cnt--) {
  119. s32_t lsample = *(inputptr++);
  120. s32_t rsample = *(inputptr++);
  121. *(optr++) =
  122. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  123. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  124. }
  125. #endif
  126. }
  127. break;
  128. case U16_BE:
  129. {
  130. u32_t *optr = (u32_t *)(void *)outputptr;
  131. #if SL_LITTLE_ENDIAN
  132. while (cnt--) {
  133. s32_t lsample = *(inputptr++);
  134. s32_t rsample = *(inputptr++);
  135. *(optr++) =
  136. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  137. (rsample & 0xff000000) >> 8 | (rsample & 0x00ff0000) << 8;
  138. }
  139. #else
  140. while (cnt--) {
  141. *(optr++) = (*(inputptr) & 0xffff0000) | (*(inputptr + 1) >> 16 & 0x0000ffff);
  142. inputptr += 2;
  143. }
  144. #endif
  145. }
  146. break;
  147. case U8:
  148. {
  149. u16_t *optr = (u16_t *)(void *)outputptr;
  150. #if SL_LITTLE_ENDIAN
  151. while (cnt--) {
  152. *(optr++) = (u16_t)((*(inputptr) >> 24 & 0x000000ff) | (*(inputptr + 1) >> 16 & 0x0000ff00));
  153. inputptr += 2;
  154. }
  155. #else
  156. while (cnt--) {
  157. *(optr++) = (u16_t)((*(inputptr) >> 16 & 0x0000ff00) | (*(inputptr + 1) >> 24 & 0x000000ff));
  158. inputptr += 2;
  159. }
  160. #endif
  161. }
  162. break;
  163. #endif
  164. case S16_LE:
  165. {
  166. u32_t *optr = (u32_t *)(void *)outputptr;
  167. #if SL_LITTLE_ENDIAN
  168. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  169. while (cnt--) {
  170. *(optr++) = (*(inputptr) >> 16 & 0x0000ffff) | (*(inputptr + 1) & 0xffff0000);
  171. inputptr += 2;
  172. }
  173. } else {
  174. while (cnt--) {
  175. *(optr++) = (gain(gainL, *(inputptr)) >> 16 & 0x0000ffff) | (gain(gainR, *(inputptr+1)) & 0xffff0000);
  176. inputptr += 2;
  177. }
  178. }
  179. #else
  180. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  181. while (cnt--) {
  182. s32_t lsample = *(inputptr++);
  183. s32_t rsample = *(inputptr++);
  184. *(optr++) =
  185. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  186. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  187. }
  188. } else {
  189. while (cnt--) {
  190. s32_t lsample = gain(gainL, *(inputptr++));
  191. s32_t rsample = gain(gainR, *(inputptr++));
  192. *(optr++) =
  193. (lsample & 0x00ff0000) << 8 | (lsample & 0xff000000) >> 8 |
  194. (rsample & 0x00ff0000) >> 8 | (rsample & 0xff000000) >> 24;
  195. }
  196. }
  197. #endif
  198. }
  199. break;
  200. case S24_LE:
  201. {
  202. u32_t *optr = (u32_t *)(void *)outputptr;
  203. #if SL_LITTLE_ENDIAN
  204. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  205. while (cnt--) {
  206. *(optr++) = *(inputptr++) >> 8;
  207. *(optr++) = *(inputptr++) >> 8;
  208. }
  209. } else {
  210. while (cnt--) {
  211. *(optr++) = gain(gainL, *(inputptr++)) >> 8;
  212. *(optr++) = gain(gainR, *(inputptr++)) >> 8;
  213. }
  214. }
  215. #else
  216. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  217. while (cnt--) {
  218. s32_t lsample = *(inputptr++);
  219. s32_t rsample = *(inputptr++);
  220. *(optr++) =
  221. (lsample & 0xff000000) >> 16 | (lsample & 0x00ff0000) | (lsample & 0x0000ff00 << 16);
  222. *(optr++) =
  223. (rsample & 0xff000000) >> 16 | (rsample & 0x00ff0000) | (rsample & 0x0000ff00 << 16);
  224. }
  225. } else {
  226. while (cnt--) {
  227. s32_t lsample = gain(gainL, *(inputptr++));
  228. s32_t rsample = gain(gainR, *(inputptr++));
  229. *(optr++) =
  230. (lsample & 0xff000000) >> 16 | (lsample & 0x00ff0000) | (lsample & 0x0000ff00 << 16);
  231. *(optr++) =
  232. (rsample & 0xff000000) >> 16 | (rsample & 0x00ff0000) | (rsample & 0x0000ff00 << 16);
  233. }
  234. }
  235. #endif
  236. }
  237. break;
  238. case S24_3LE:
  239. {
  240. u8_t *optr = (u8_t *)(void *)outputptr;
  241. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  242. while (cnt) {
  243. // attempt to do 32 bit memory accesses - move 2 frames at once: 16 bytes -> 12 bytes
  244. // falls through to exception case when not aligned or if less than 2 frames to move
  245. if (((uintptr_t)optr & 0x3) == 0 && cnt >= 2) {
  246. u32_t *o_ptr = (u32_t *)(void *)optr;
  247. while (cnt >= 2) {
  248. s32_t l1 = *(inputptr++); s32_t r1 = *(inputptr++);
  249. s32_t l2 = *(inputptr++); s32_t r2 = *(inputptr++);
  250. #if SL_LITTLE_ENDIAN
  251. *(o_ptr++) = (l1 & 0xffffff00) >> 8 | (r1 & 0x0000ff00) << 16;
  252. *(o_ptr++) = (r1 & 0xffff0000) >> 16 | (l2 & 0x00ffff00) << 8;
  253. *(o_ptr++) = (l2 & 0xff000000) >> 24 | (r2 & 0xffffff00);
  254. #else
  255. *(o_ptr++) = (l1 & 0x0000ff00) << 16 | (l1 & 0x00ff0000) | (l1 & 0xff000000) >> 16 |
  256. (r1 & 0x0000ff00) >> 8;
  257. *(o_ptr++) = (r1 & 0x00ff0000) << 8 | (r1 & 0xff000000) >> 8 | (l2 & 0x0000ff00) |
  258. (l2 & 0x00ff0000) >> 16;
  259. *(o_ptr++) = (l2 & 0xff000000) | (r2 & 0x0000ff00) << 8 | (r2 & 0x00ff0000) >> 8 |
  260. (r2 & 0xff000000) >> 24;
  261. #endif
  262. optr += 12;
  263. cnt -= 2;
  264. }
  265. } else {
  266. s32_t lsample = *(inputptr++);
  267. s32_t rsample = *(inputptr++);
  268. *(optr++) = (lsample & 0x0000ff00) >> 8;
  269. *(optr++) = (lsample & 0x00ff0000) >> 16;
  270. *(optr++) = (lsample & 0xff000000) >> 24;
  271. *(optr++) = (rsample & 0x0000ff00) >> 8;
  272. *(optr++) = (rsample & 0x00ff0000) >> 16;
  273. *(optr++) = (rsample & 0xff000000) >> 24;
  274. cnt--;
  275. }
  276. }
  277. } else {
  278. while (cnt) {
  279. // attempt to do 32 bit memory accesses - move 2 frames at once: 16 bytes -> 12 bytes
  280. // falls through to exception case when not aligned or if less than 2 frames to move
  281. if (((uintptr_t)optr & 0x3) == 0 && cnt >= 2) {
  282. u32_t *o_ptr = (u32_t *)(void *)optr;
  283. while (cnt >= 2) {
  284. s32_t l1 = gain(gainL, *(inputptr++)); s32_t r1 = gain(gainR, *(inputptr++));
  285. s32_t l2 = gain(gainL, *(inputptr++)); s32_t r2 = gain(gainR, *(inputptr++));
  286. #if SL_LITTLE_ENDIAN
  287. *(o_ptr++) = (l1 & 0xffffff00) >> 8 | (r1 & 0x0000ff00) << 16;
  288. *(o_ptr++) = (r1 & 0xffff0000) >> 16 | (l2 & 0x00ffff00) << 8;
  289. *(o_ptr++) = (l2 & 0xff000000) >> 24 | (r2 & 0xffffff00);
  290. #else
  291. *(o_ptr++) = (l1 & 0x0000ff00) << 16 | (l1 & 0x00ff0000) | (l1 & 0xff000000) >> 16 |
  292. (r1 & 0x0000ff00) >> 8;
  293. *(o_ptr++) = (r1 & 0x00ff0000) << 8 | (r1 & 0xff000000) >> 8 | (l2 & 0x0000ff00) |
  294. (l2 & 0x00ff0000) >> 16;
  295. *(o_ptr++) = (l2 & 0xff000000) | (r2 & 0x0000ff00) << 8 | (r2 & 0x00ff0000) >> 8 |
  296. (r2 & 0xff000000) >> 24;
  297. #endif
  298. optr += 12;
  299. cnt -= 2;
  300. }
  301. } else {
  302. s32_t lsample = gain(gainL, *(inputptr++));
  303. s32_t rsample = gain(gainR, *(inputptr++));
  304. *(optr++) = (lsample & 0x0000ff00) >> 8;
  305. *(optr++) = (lsample & 0x00ff0000) >> 16;
  306. *(optr++) = (lsample & 0xff000000) >> 24;
  307. *(optr++) = (rsample & 0x0000ff00) >> 8;
  308. *(optr++) = (rsample & 0x00ff0000) >> 16;
  309. *(optr++) = (rsample & 0xff000000) >> 24;
  310. cnt--;
  311. }
  312. }
  313. }
  314. }
  315. break;
  316. case S32_LE:
  317. {
  318. u32_t *optr = (u32_t *)(void *)outputptr;
  319. #if SL_LITTLE_ENDIAN
  320. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  321. memcpy(outputptr, inputptr, cnt * BYTES_PER_FRAME);
  322. } else {
  323. while (cnt--) {
  324. *(optr++) = gain(gainL, *(inputptr++));
  325. *(optr++) = gain(gainR, *(inputptr++));
  326. }
  327. }
  328. #else
  329. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  330. while (cnt--) {
  331. s32_t lsample = *(inputptr++);
  332. s32_t rsample = *(inputptr++);
  333. *(optr++) =
  334. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  335. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  336. *(optr++) =
  337. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  338. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  339. }
  340. } else {
  341. while (cnt--) {
  342. s32_t lsample = gain(gainL, *(inputptr++));
  343. s32_t rsample = gain(gainR, *(inputptr++));
  344. *(optr++) =
  345. (lsample & 0xff000000) >> 24 | (lsample & 0x00ff0000) >> 8 |
  346. (lsample & 0x0000ff00) << 8 | (lsample & 0x000000ff) << 24;
  347. *(optr++) =
  348. (rsample & 0xff000000) >> 24 | (rsample & 0x00ff0000) >> 8 |
  349. (rsample & 0x0000ff00) << 8 | (rsample & 0x000000ff) << 24;
  350. }
  351. }
  352. #endif
  353. }
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. #if !WIN
  360. inline
  361. #endif
  362. void _apply_cross(struct buffer *outputbuf, frames_t out_frames, s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  363. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp;
  364. frames_t count = out_frames * 2;
  365. while (count--) {
  366. if (*cross_ptr > (ISAMPLE_T *)outputbuf->wrap) {
  367. *cross_ptr -= outputbuf->size / BYTES_PER_FRAME * 2;
  368. }
  369. *ptr = gain(cross_gain_out, *ptr) + gain(cross_gain_in, **cross_ptr);
  370. ptr++; (*cross_ptr)++;
  371. }
  372. }
  373. #if !WIN
  374. inline
  375. #endif
  376. void _apply_gain(struct buffer *outputbuf, frames_t count, s32_t gainL, s32_t gainR) {
  377. if (gainL == FIXED_ONE && gainR == FIXED_ONE) {
  378. return;
  379. } if ((gainR & MONO_FLAG) && (gainL & MONO_FLAG)) {
  380. ISAMPLE_T *ptrL = (ISAMPLE_T *)(void *)outputbuf->readp;
  381. ISAMPLE_T *ptrR = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  382. gainL &= ~MONO_FLAG; gainR &= ~MONO_FLAG;
  383. while (count--) {
  384. *ptrL = *ptrR = (gain(gainL, *ptrL) + gain(gainR, *ptrR)) / 2;
  385. ptrL += 2; ptrR += 2;
  386. }
  387. } else if (gainL & MONO_FLAG) {
  388. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  389. while (count--) {
  390. *(ptr - 1) = *ptr = gain(gainR, *ptr);
  391. ptr += 2;
  392. }
  393. } else if (gainR & MONO_FLAG) {
  394. ISAMPLE_T *ptr = (ISAMPLE_T *)(void *)outputbuf->readp;
  395. while (count--) {
  396. *(ptr + 1) = *ptr = gain(gainL, *ptr);
  397. ptr += 2;
  398. }
  399. } else {
  400. ISAMPLE_T *ptrL = (ISAMPLE_T *)(void *)outputbuf->readp;
  401. ISAMPLE_T *ptrR = (ISAMPLE_T *)(void *)outputbuf->readp + 1;
  402. while (count--) {
  403. *ptrL = gain(gainL, *ptrL);
  404. *ptrR = gain(gainR, *ptrR);
  405. ptrL += 2; ptrR += 2;
  406. }
  407. }
  408. }