alsa: Add a proplist to mappings
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / sconv-s16le.c
blob138e41857cccb5fa6894753493fbb698bb513e34
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 /* Despite the name of this file we implement S32 and S24 handling here, too. */
28 #include <inttypes.h>
29 #include <stdio.h>
30 #include <math.h>
32 #include <pulsecore/sconv.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/endianmacros.h>
36 #include "sconv-s16le.h"
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
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
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
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
66 #ifndef READ24
67 #define READ24 PA_READ24LE
68 #endif
69 #ifndef WRITE24
70 #define WRITE24 PA_WRITE24LE
71 #endif
73 #ifndef SWAP_WORDS
74 #ifdef WORDS_BIGENDIAN
75 #define SWAP_WORDS 1
76 #else
77 #define SWAP_WORDS 0
78 #endif
79 #endif
81 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
82 pa_assert(a);
83 pa_assert(b);
85 #if SWAP_WORDS == 1
86 for (; n > 0; n--) {
87 int16_t s = *(a++);
88 *(b++) = ((float) INT16_FROM(s))/(float) 0x7FFF;
90 #else
91 for (; n > 0; n--)
92 *(b++) = ((float) (*(a++)))/(float) 0x7FFF;
93 #endif
96 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
97 pa_assert(a);
98 pa_assert(b);
100 #if SWAP_WORDS == 1
101 for (; n > 0; n--) {
102 int32_t s = *(a++);
103 *(b++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
105 #else
106 for (; n > 0; n--)
107 *(b++) = (float) (((double) (*(a++)))/0x7FFFFFFF);
108 #endif
111 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
112 pa_assert(a);
113 pa_assert(b);
115 #if SWAP_WORDS == 1
116 for (; n > 0; n--) {
117 int16_t s;
118 float v = *(a++);
120 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
121 s = (int16_t) lrintf(v * 0x7FFF);
122 *(b++) = INT16_TO(s);
124 #else
125 for (; n > 0; n--) {
126 float v = *(a++);
128 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
129 *(b++) = (int16_t) lrintf(v * 0x7FFF);
131 #endif
134 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
135 pa_assert(a);
136 pa_assert(b);
138 #if SWAP_WORDS == 1
139 for (; n > 0; n--) {
140 int32_t s;
141 float v = *(a++);
143 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
144 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
145 *(b++) = INT32_TO(s);
147 #else
148 for (; n > 0; n--) {
149 float v = *(a++);
151 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
152 *(b++) = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
154 #endif
157 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
158 pa_assert(a);
159 pa_assert(b);
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;
169 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
170 pa_assert(a);
171 pa_assert(b);
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;
181 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
182 pa_assert(a);
183 pa_assert(b);
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);
195 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
196 pa_assert(a);
197 pa_assert(b);
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);
209 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
210 pa_assert(a);
211 pa_assert(b);
213 for (; n > 0; n--) {
214 *b = (int16_t) (INT32_FROM(*a) >> 16);
215 a++;
216 b++;
220 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
221 pa_assert(a);
222 pa_assert(b);
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++;
232 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
233 pa_assert(a);
234 pa_assert(b);
236 for (; n > 0; n--) {
237 *b = INT32_TO(((int32_t) *a) << 16);
238 a++;
239 b++;
243 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
244 pa_assert(a);
245 pa_assert(b);
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++;
255 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
256 pa_assert(a);
257 pa_assert(b);
259 for (; n > 0; n--) {
260 *b = (int16_t) (READ24(a) >> 8);
261 a += 3;
262 b++;
266 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
267 pa_assert(a);
268 pa_assert(b);
270 for (; n > 0; n--) {
271 WRITE24(b, ((uint32_t) *a) << 8);
272 a++;
273 b += 3;
277 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
278 pa_assert(a);
279 pa_assert(b);
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++;
289 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
290 pa_assert(a);
291 pa_assert(b);
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;
301 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
302 pa_assert(a);
303 pa_assert(b);
305 for (; n > 0; n--) {
306 int32_t s = READ24(a) << 8;
307 *b = ((float) s) / 0x7FFFFFFF;
308 a += 3;
309 b ++;
313 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
314 pa_assert(a);
315 pa_assert(b);
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;
328 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
329 pa_assert(a);
330 pa_assert(b);
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 ++;
341 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
342 pa_assert(a);
343 pa_assert(b);
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;
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);
361 for (; n > 0; n--) {
362 *b = (int16_t) (((int32_t) (UINT32_FROM(*a) << 8)) >> 16);
363 a++;
364 b++;
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);
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++;
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);
384 for (; n > 0; n--) {
385 *b = UINT32_TO(((uint32_t) ((int32_t) *a << 16)) >> 8);
386 a++;
387 b++;
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);
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++;
403 void pa_sconv_s24_32le_to_float32ne(unsigned n, const uint32_t *a, float *b) {
404 pa_assert(a);
405 pa_assert(b);
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 ++;
415 void pa_sconv_s24_32le_to_float32re(unsigned n, const uint32_t *a, float *b) {
416 pa_assert(a);
417 pa_assert(b);
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 ++;
428 void pa_sconv_s24_32le_from_float32ne(unsigned n, const float *a, uint32_t *b) {
429 pa_assert(a);
430 pa_assert(b);
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++;
443 void pa_sconv_s24_32le_from_float32re(unsigned n, const float *a, uint32_t *b) {
444 pa_assert(a);
445 pa_assert(b);
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++;