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