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