]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
Remove unnecessary #includes
[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++) = ((float) INT16_FROM(s))/(float) 0x7FFF;
89 }
90 #else
91 for (; n > 0; n--)
92 *(b++) = ((float) (*(a++)))/(float) 0x7FFF;
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++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
104 }
105 #else
106 for (; n > 0; n--)
107 *(b++) = (float) (((double) (*(a++)))/0x7FFFFFFF);
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++);
119
120 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
121 s = (int16_t) lrintf(v * 0x7FFF);
122 *(b++) = INT16_TO(s);
123 }
124 #else
125 for (; n > 0; n--) {
126 float v = *(a++);
127
128 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
129 *(b++) = (int16_t) lrintf(v * 0x7FFF);
130 }
131 #endif
132 }
133
134 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
135 pa_assert(a);
136 pa_assert(b);
137
138 #if SWAP_WORDS == 1
139 for (; n > 0; n--) {
140 int32_t s;
141 float v = *(a++);
142
143 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
144 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
145 *(b++) = INT32_TO(s);
146 }
147 #else
148 for (; n > 0; n--) {
149 float v = *(a++);
150
151 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
152 *(b++) = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
153 }
154 #endif
155 }
156
157 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
158 pa_assert(a);
159 pa_assert(b);
160
161 for (; n > 0; n--) {
162 int16_t s = *(a++);
163 float k = ((float) INT16_FROM(s))/0x7FFF;
164 k = PA_FLOAT32_SWAP(k);
165 *(b++) = k;
166 }
167 }
168
169 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
170 pa_assert(a);
171 pa_assert(b);
172
173 for (; n > 0; n--) {
174 int32_t s = *(a++);
175 float k = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
176 k = PA_FLOAT32_SWAP(k);
177 *(b++) = k;
178 }
179 }
180
181 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
182 pa_assert(a);
183 pa_assert(b);
184
185 for (; n > 0; n--) {
186 int16_t s;
187 float v = *(a++);
188 v = PA_FLOAT32_SWAP(v);
189 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
190 s = (int16_t) lrintf(v * 0x7FFF);
191 *(b++) = INT16_TO(s);
192 }
193 }
194
195 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
196 pa_assert(a);
197 pa_assert(b);
198
199 for (; n > 0; n--) {
200 int32_t s;
201 float v = *(a++);
202 v = PA_FLOAT32_SWAP(v);
203 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
204 s = (int32_t) lrint((double) v * 0x7FFFFFFF);
205 *(b++) = INT32_TO(s);
206 }
207 }
208
209 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
210 pa_assert(a);
211 pa_assert(b);
212
213 for (; n > 0; n--) {
214 *b = (int16_t) (INT32_FROM(*a) >> 16);
215 a++;
216 b++;
217 }
218 }
219
220 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
221 pa_assert(a);
222 pa_assert(b);
223
224 for (; n > 0; n--) {
225 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
226 *b = PA_INT16_SWAP(s);
227 a++;
228 b++;
229 }
230 }
231
232 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
233 pa_assert(a);
234 pa_assert(b);
235
236 for (; n > 0; n--) {
237 *b = INT32_TO(((int32_t) *a) << 16);
238 a++;
239 b++;
240 }
241 }
242
243 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
244 pa_assert(a);
245 pa_assert(b);
246
247 for (; n > 0; n--) {
248 int32_t s = ((int32_t) PA_INT16_SWAP(*a)) << 16;
249 *b = INT32_TO(s);
250 a++;
251 b++;
252 }
253 }
254
255 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
256 pa_assert(a);
257 pa_assert(b);
258
259 for (; n > 0; n--) {
260 *b = (int16_t) (READ24(a) >> 8);
261 a += 3;
262 b++;
263 }
264 }
265
266 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
267 pa_assert(a);
268 pa_assert(b);
269
270 for (; n > 0; n--) {
271 WRITE24(b, ((uint32_t) *a) << 8);
272 a++;
273 b += 3;
274 }
275 }
276
277 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
278 pa_assert(a);
279 pa_assert(b);
280
281 for (; n > 0; n--) {
282 int16_t s = (int16_t) (READ24(a) >> 8);
283 *b = PA_INT16_SWAP(s);
284 a += 3;
285 b++;
286 }
287 }
288
289 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
290 pa_assert(a);
291 pa_assert(b);
292
293 for (; n > 0; n--) {
294 uint32_t s = ((uint32_t) PA_INT16_SWAP(*a)) << 8;
295 WRITE24(b, s);
296 a++;
297 b += 3;
298 }
299 }
300
301 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
302 pa_assert(a);
303 pa_assert(b);
304
305 for (; n > 0; n--) {
306 int32_t s = READ24(a) << 8;
307 *b = ((float) s) / 0x7FFFFFFF;
308 a += 3;
309 b ++;
310 }
311 }
312
313 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
314 pa_assert(a);
315 pa_assert(b);
316
317 for (; n > 0; n--) {
318 int32_t s;
319 float v = *a;
320 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
321 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
322 WRITE24(b, ((uint32_t) s) >> 8);
323 a++;
324 b+=3;
325 }
326 }
327
328 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
329 pa_assert(a);
330 pa_assert(b);
331
332 for (; n > 0; n--) {
333 int32_t s = READ24(a) << 8;
334 float k = ((float) s) / 0x7FFFFFFF;
335 *b = PA_FLOAT32_SWAP(k);
336 a += 3;
337 b ++;
338 }
339 }
340
341 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
342 pa_assert(a);
343 pa_assert(b);
344
345 for (; n > 0; n--) {
346 int32_t s;
347 float v = *a;
348 v = PA_FLOAT32_SWAP(v);
349 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
350 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
351 WRITE24(b, ((uint32_t) s) >> 8);
352 a++;
353 b+=3;
354 }
355 }
356
357 void pa_sconv_s24_32le_to_s16ne(unsigned n, const uint32_t *a, int16_t *b) {
358 pa_assert(a);
359 pa_assert(b);
360
361 for (; n > 0; n--) {
362 *b = (int16_t) (((int32_t) (UINT32_FROM(*a) << 8)) >> 16);
363 a++;
364 b++;
365 }
366 }
367
368 void pa_sconv_s24_32le_to_s16re(unsigned n, const uint32_t *a, int16_t *b) {
369 pa_assert(a);
370 pa_assert(b);
371
372 for (; n > 0; n--) {
373 int16_t s = (int16_t) ((int32_t) (UINT32_FROM(*a) << 8) >> 16);
374 *b = PA_INT16_SWAP(s);
375 a++;
376 b++;
377 }
378 }
379
380 void pa_sconv_s24_32le_from_s16ne(unsigned n, const int16_t *a, uint32_t *b) {
381 pa_assert(a);
382 pa_assert(b);
383
384 for (; n > 0; n--) {
385 *b = UINT32_TO(((uint32_t) ((int32_t) *a << 16)) >> 8);
386 a++;
387 b++;
388 }
389 }
390
391 void pa_sconv_s24_32le_from_s16re(unsigned n, const int16_t *a, uint32_t *b) {
392 pa_assert(a);
393 pa_assert(b);
394
395 for (; n > 0; n--) {
396 uint32_t s = ((uint32_t) ((int32_t) PA_INT16_SWAP(*a) << 16)) >> 8;
397 *b = UINT32_TO(s);
398 a++;
399 b++;
400 }
401 }
402
403 void pa_sconv_s24_32le_to_float32ne(unsigned n, const uint32_t *a, float *b) {
404 pa_assert(a);
405 pa_assert(b);
406
407 for (; n > 0; n--) {
408 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
409 *b = (float) s / (float) 0x7FFFFFFF;
410 a ++;
411 b ++;
412 }
413 }
414
415 void pa_sconv_s24_32le_to_float32re(unsigned n, const uint32_t *a, float *b) {
416 pa_assert(a);
417 pa_assert(b);
418
419 for (; n > 0; n--) {
420 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
421 float k = (float) s / (float) 0x7FFFFFFF;
422 *b = PA_FLOAT32_SWAP(k);
423 a ++;
424 b ++;
425 }
426 }
427
428 void pa_sconv_s24_32le_from_float32ne(unsigned n, const float *a, uint32_t *b) {
429 pa_assert(a);
430 pa_assert(b);
431
432 for (; n > 0; n--) {
433 int32_t s;
434 float v = *a;
435 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
436 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
437 *b = UINT32_TO(((uint32_t) s) >> 8);
438 a++;
439 b++;
440 }
441 }
442
443 void pa_sconv_s24_32le_from_float32re(unsigned n, const float *a, uint32_t *b) {
444 pa_assert(a);
445 pa_assert(b);
446
447 for (; n > 0; n--) {
448 int32_t s;
449 float v = *a;
450 v = PA_FLOAT32_SWAP(v);
451 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
452 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
453 *b = UINT32_TO(((uint32_t) s) >> 8);
454 a++;
455 b++;
456 }
457 }