]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / sconv-s16le.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 /* Despite the name of this file we implement S32 and S24 handling here, too. */
27
28 #include <inttypes.h>
29 #include <stdio.h>
30 #include <math.h>
31
32 #include <pulsecore/sconv.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/endianmacros.h>
35
36 #include "sconv-s16le.h"
37
38 #ifndef INT16_FROM
39 #define INT16_FROM PA_INT16_FROM_LE
40 #endif
41 #ifndef UINT16_FROM
42 #define UINT16_FROM PA_UINT16_FROM_LE
43 #endif
44
45 #ifndef INT16_TO
46 #define INT16_TO PA_INT16_TO_LE
47 #endif
48 #ifndef UINT16_TO
49 #define UINT16_TO PA_UINT16_TO_LE
50 #endif
51
52 #ifndef INT32_FROM
53 #define INT32_FROM PA_INT32_FROM_LE
54 #endif
55 #ifndef UINT32_FROM
56 #define UINT32_FROM PA_UINT32_FROM_LE
57 #endif
58
59 #ifndef INT32_TO
60 #define INT32_TO PA_INT32_TO_LE
61 #endif
62 #ifndef UINT32_TO
63 #define UINT32_TO PA_UINT32_TO_LE
64 #endif
65
66 #ifndef READ24
67 #define READ24 PA_READ24LE
68 #endif
69 #ifndef WRITE24
70 #define WRITE24 PA_WRITE24LE
71 #endif
72
73 #ifndef SWAP_WORDS
74 #ifdef WORDS_BIGENDIAN
75 #define SWAP_WORDS 1
76 #else
77 #define SWAP_WORDS 0
78 #endif
79 #endif
80
81 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
82 pa_assert(a);
83 pa_assert(b);
84
85 #if SWAP_WORDS == 1
86 for (; n > 0; n--) {
87 int16_t s = *(a++);
88 *(b++) = INT16_FROM(s) * (1.0f / (1 << 15));
89 }
90 #else
91 for (; n > 0; n--)
92 *(b++) = *(a++) * (1.0f / (1 << 15));
93 #endif
94 }
95
96 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
97 pa_assert(a);
98 pa_assert(b);
99
100 #if SWAP_WORDS == 1
101 for (; n > 0; n--) {
102 int32_t s = *(a++);
103 *(b++) = INT32_FROM(s) * (1.0f / (1U << 31));
104 }
105 #else
106 for (; n > 0; n--)
107 *(b++) = *(a++) * (1.0f / (1U << 31));
108 #endif
109 }
110
111 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
112 pa_assert(a);
113 pa_assert(b);
114
115 #if SWAP_WORDS == 1
116 for (; n > 0; n--) {
117 int16_t s;
118 float v = *(a++) * (1 << 15);
119
120 s = (int16_t) PA_CLAMP_UNLIKELY(lrintf(v), -0x8000, 0x7FFF);
121 *(b++) = INT16_TO(s);
122 }
123 #else
124 for (; n > 0; n--) {
125 float v = *(a++) * (1 << 15);
126
127 *(b++) = (int16_t) PA_CLAMP_UNLIKELY(lrintf(v), -0x8000, 0x7FFF);
128 }
129 #endif
130 }
131
132 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
133 pa_assert(a);
134 pa_assert(b);
135
136 #if SWAP_WORDS == 1
137 for (; n > 0; n--) {
138 int32_t s;
139 float v = *(a++) * (1U << 31);
140
141 s = (int32_t) PA_CLAMP_UNLIKELY(llrintf(v), -0x80000000LL, 0x7FFFFFFFLL);
142 *(b++) = INT32_TO(s);
143 }
144 #else
145 for (; n > 0; n--) {
146 float v = *(a++) * (1U << 31);
147
148 *(b++) = (int32_t) PA_CLAMP_UNLIKELY(llrintf(v), -0x80000000LL, 0x7FFFFFFFLL);
149 }
150 #endif
151 }
152
153 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
154 pa_assert(a);
155 pa_assert(b);
156
157 for (; n > 0; n--) {
158 int16_t s = *(a++);
159 float k = INT16_FROM(s) * (1.0f / (1 << 15));
160 k = PA_FLOAT32_SWAP(k);
161 *(b++) = k;
162 }
163 }
164
165 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
166 pa_assert(a);
167 pa_assert(b);
168
169 for (; n > 0; n--) {
170 int32_t s = *(a++);
171 float k = INT32_FROM(s) * (1.0f / (1U << 31));
172 k = PA_FLOAT32_SWAP(k);
173 *(b++) = k;
174 }
175 }
176
177 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
178 pa_assert(a);
179 pa_assert(b);
180
181 for (; n > 0; n--) {
182 int16_t s;
183 float v = *(a++);
184 v = PA_FLOAT32_SWAP(v) * (1 << 15);
185 s = (int16_t) PA_CLAMP_UNLIKELY(lrintf(v), -0x8000, 0x7FFF);
186 *(b++) = INT16_TO(s);
187 }
188 }
189
190 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
191 pa_assert(a);
192 pa_assert(b);
193
194 for (; n > 0; n--) {
195 int32_t s;
196 float v = *(a++);
197 v = PA_FLOAT32_SWAP(v) * (1U << 31);
198 s = (int32_t) PA_CLAMP_UNLIKELY(llrintf(v), -0x80000000LL, 0x7FFFFFFFLL);
199 *(b++) = INT32_TO(s);
200 }
201 }
202
203 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
204 pa_assert(a);
205 pa_assert(b);
206
207 for (; n > 0; n--) {
208 *b = (int16_t) (INT32_FROM(*a) >> 16);
209 a++;
210 b++;
211 }
212 }
213
214 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
215 pa_assert(a);
216 pa_assert(b);
217
218 for (; n > 0; n--) {
219 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
220 *b = PA_INT16_SWAP(s);
221 a++;
222 b++;
223 }
224 }
225
226 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
227 pa_assert(a);
228 pa_assert(b);
229
230 for (; n > 0; n--) {
231 *b = INT32_TO(((int32_t) *a) << 16);
232 a++;
233 b++;
234 }
235 }
236
237 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
238 pa_assert(a);
239 pa_assert(b);
240
241 for (; n > 0; n--) {
242 int32_t s = ((int32_t) PA_INT16_SWAP(*a)) << 16;
243 *b = INT32_TO(s);
244 a++;
245 b++;
246 }
247 }
248
249 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
250 pa_assert(a);
251 pa_assert(b);
252
253 for (; n > 0; n--) {
254 *b = (int16_t) (READ24(a) >> 8);
255 a += 3;
256 b++;
257 }
258 }
259
260 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
261 pa_assert(a);
262 pa_assert(b);
263
264 for (; n > 0; n--) {
265 WRITE24(b, ((uint32_t) *a) << 8);
266 a++;
267 b += 3;
268 }
269 }
270
271 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
272 pa_assert(a);
273 pa_assert(b);
274
275 for (; n > 0; n--) {
276 int16_t s = (int16_t) (READ24(a) >> 8);
277 *b = PA_INT16_SWAP(s);
278 a += 3;
279 b++;
280 }
281 }
282
283 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
284 pa_assert(a);
285 pa_assert(b);
286
287 for (; n > 0; n--) {
288 uint32_t s = ((uint32_t) PA_INT16_SWAP(*a)) << 8;
289 WRITE24(b, s);
290 a++;
291 b += 3;
292 }
293 }
294
295 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
296 pa_assert(a);
297 pa_assert(b);
298
299 for (; n > 0; n--) {
300 int32_t s = READ24(a) << 8;
301 *b = s * (1.0f / (1U << 31));
302 a += 3;
303 b++;
304 }
305 }
306
307 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
308 pa_assert(a);
309 pa_assert(b);
310
311 for (; n > 0; n--) {
312 int32_t s;
313 float v = *a * (1U << 31);
314 s = (int32_t) PA_CLAMP_UNLIKELY(llrint(v), -0x80000000LL, 0x7FFFFFFFLL);
315 WRITE24(b, ((uint32_t) s) >> 8);
316 a++;
317 b += 3;
318 }
319 }
320
321 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
322 pa_assert(a);
323 pa_assert(b);
324
325 for (; n > 0; n--) {
326 int32_t s = READ24(a) << 8;
327 float k = s * (1.0f / (1U << 31));
328 *b = PA_FLOAT32_SWAP(k);
329 a += 3;
330 b++;
331 }
332 }
333
334 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
335 pa_assert(a);
336 pa_assert(b);
337
338 for (; n > 0; n--) {
339 int32_t s;
340 float v = *a;
341 v = PA_FLOAT32_SWAP(v) * (1U << 31);
342 s = (int32_t) PA_CLAMP_UNLIKELY(llrint(v), -0x80000000LL, 0x7FFFFFFFLL);
343 WRITE24(b, ((uint32_t) s) >> 8);
344 a++;
345 b+=3;
346 }
347 }
348
349 void pa_sconv_s24_32le_to_s16ne(unsigned n, const uint32_t *a, int16_t *b) {
350 pa_assert(a);
351 pa_assert(b);
352
353 for (; n > 0; n--) {
354 *b = (int16_t) (((int32_t) (UINT32_FROM(*a) << 8)) >> 16);
355 a++;
356 b++;
357 }
358 }
359
360 void pa_sconv_s24_32le_to_s16re(unsigned n, const uint32_t *a, int16_t *b) {
361 pa_assert(a);
362 pa_assert(b);
363
364 for (; n > 0; n--) {
365 int16_t s = (int16_t) ((int32_t) (UINT32_FROM(*a) << 8) >> 16);
366 *b = PA_INT16_SWAP(s);
367 a++;
368 b++;
369 }
370 }
371
372 void pa_sconv_s24_32le_from_s16ne(unsigned n, const int16_t *a, uint32_t *b) {
373 pa_assert(a);
374 pa_assert(b);
375
376 for (; n > 0; n--) {
377 *b = UINT32_TO(((uint32_t) ((int32_t) *a << 16)) >> 8);
378 a++;
379 b++;
380 }
381 }
382
383 void pa_sconv_s24_32le_from_s16re(unsigned n, const int16_t *a, uint32_t *b) {
384 pa_assert(a);
385 pa_assert(b);
386
387 for (; n > 0; n--) {
388 uint32_t s = ((uint32_t) ((int32_t) PA_INT16_SWAP(*a) << 16)) >> 8;
389 *b = UINT32_TO(s);
390 a++;
391 b++;
392 }
393 }
394
395 void pa_sconv_s24_32le_to_float32ne(unsigned n, const uint32_t *a, float *b) {
396 pa_assert(a);
397 pa_assert(b);
398
399 for (; n > 0; n--) {
400 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
401 *b = s * (1.0f / (1U << 31));
402 a++;
403 b++;
404 }
405 }
406
407 void pa_sconv_s24_32le_to_float32re(unsigned n, const uint32_t *a, float *b) {
408 pa_assert(a);
409 pa_assert(b);
410
411 for (; n > 0; n--) {
412 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
413 float k = s * (1.0f / (1U << 31));
414 *b = PA_FLOAT32_SWAP(k);
415 a++;
416 b++;
417 }
418 }
419
420 void pa_sconv_s24_32le_from_float32ne(unsigned n, const float *a, uint32_t *b) {
421 pa_assert(a);
422 pa_assert(b);
423
424 for (; n > 0; n--) {
425 int32_t s;
426 float v = *a * (1U << 31);
427 s = (int32_t) PA_CLAMP_UNLIKELY(llrint(v), -0x80000000LL, 0x7FFFFFFFLL);
428 *b = UINT32_TO(((uint32_t) s) >> 8);
429 a++;
430 b++;
431 }
432 }
433
434 void pa_sconv_s24_32le_from_float32re(unsigned n, const float *a, uint32_t *b) {
435 pa_assert(a);
436 pa_assert(b);
437
438 for (; n > 0; n--) {
439 int32_t s;
440 float v = *a;
441 v = PA_FLOAT32_SWAP(v) * (1U << 31);
442 s = (int32_t) PA_CLAMP_UNLIKELY(llrint(v), -0x80000000LL, 0x7FFFFFFFLL);
443 *b = UINT32_TO(((uint32_t) s) >> 8);
444 a++;
445 b++;
446 }
447 }