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
26 /* Despite the name of this file we implement S32 and S24 handling here, too. */
32 #include <pulsecore/sconv.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/endianmacros.h>
36 #include "sconv-s16le.h"
39 #define INT16_FROM PA_INT16_FROM_LE
42 #define UINT16_FROM PA_UINT16_FROM_LE
46 #define INT16_TO PA_INT16_TO_LE
49 #define UINT16_TO PA_UINT16_TO_LE
53 #define INT32_FROM PA_INT32_FROM_LE
56 #define UINT32_FROM PA_UINT32_FROM_LE
60 #define INT32_TO PA_INT32_TO_LE
63 #define UINT32_TO PA_UINT32_TO_LE
67 #define READ24 PA_READ24LE
70 #define WRITE24 PA_WRITE24LE
74 #ifdef WORDS_BIGENDIAN
81 void pa_sconv_s16le_to_float32ne(unsigned n
, const int16_t *a
, float *b
) {
88 *(b
++) = ((float) INT16_FROM(s
))/(float) 0x7FFF;
92 *(b
++) = ((float) (*(a
++)))/(float) 0x7FFF;
96 void pa_sconv_s32le_to_float32ne(unsigned n
, const int32_t *a
, float *b
) {
103 *(b
++) = (float) (((double) INT32_FROM(s
))/0x7FFFFFFF);
107 *(b
++) = (float) (((double) (*(a
++)))/0x7FFFFFFF);
111 void pa_sconv_s16le_from_float32ne(unsigned n
, const float *a
, int16_t *b
) {
120 v
= PA_CLAMP_UNLIKELY(v
, -1.0f
, 1.f
);
121 s
= (int16_t) lrintf(v
* 0x7FFF);
122 *(b
++) = INT16_TO(s
);
128 v
= PA_CLAMP_UNLIKELY(v
, -1.0f
, 1.f
);
129 *(b
++) = (int16_t) lrintf(v
* 0x7FFF);
134 void pa_sconv_s32le_from_float32ne(unsigned n
, const float *a
, int32_t *b
) {
143 v
= PA_CLAMP_UNLIKELY(v
, -1.0f
, 1.0f
);
144 s
= (int32_t) lrint((double) v
* (double) 0x7FFFFFFF);
145 *(b
++) = INT32_TO(s
);
151 v
= PA_CLAMP_UNLIKELY(v
, -1.0f
, 1.0f
);
152 *(b
++) = (int32_t) lrint((double) v
* (double) 0x7FFFFFFF);
157 void pa_sconv_s16le_to_float32re(unsigned n
, const int16_t *a
, float *b
) {
163 float k
= ((float) INT16_FROM(s
))/0x7FFF;
164 k
= PA_FLOAT32_SWAP(k
);
169 void pa_sconv_s32le_to_float32re(unsigned n
, const int32_t *a
, float *b
) {
175 float k
= (float) (((double) INT32_FROM(s
))/0x7FFFFFFF);
176 k
= PA_FLOAT32_SWAP(k
);
181 void pa_sconv_s16le_from_float32re(unsigned n
, const float *a
, int16_t *b
) {
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
) {
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
) {
214 *b
= (int16_t) (INT32_FROM(*a
) >> 16);
220 void pa_sconv_s32le_to_s16re(unsigned n
, const int32_t*a
, int16_t *b
) {
225 int16_t s
= (int16_t) (INT32_FROM(*a
) >> 16);
226 *b
= PA_INT16_SWAP(s
);
232 void pa_sconv_s32le_from_s16ne(unsigned n
, const int16_t *a
, int32_t *b
) {
237 *b
= INT32_TO(((int32_t) *a
) << 16);
243 void pa_sconv_s32le_from_s16re(unsigned n
, const int16_t *a
, int32_t *b
) {
248 int32_t s
= ((int32_t) PA_INT16_SWAP(*a
)) << 16;
255 void pa_sconv_s24le_to_s16ne(unsigned n
, const uint8_t *a
, int16_t *b
) {
260 *b
= (int16_t) (READ24(a
) >> 8);
266 void pa_sconv_s24le_from_s16ne(unsigned n
, const int16_t *a
, uint8_t *b
) {
271 WRITE24(b
, ((uint32_t) *a
) << 8);
277 void pa_sconv_s24le_to_s16re(unsigned n
, const uint8_t *a
, int16_t *b
) {
282 int16_t s
= (int16_t) (READ24(a
) >> 8);
283 *b
= PA_INT16_SWAP(s
);
289 void pa_sconv_s24le_from_s16re(unsigned n
, const int16_t *a
, uint8_t *b
) {
294 uint32_t s
= ((uint32_t) PA_INT16_SWAP(*a
)) << 8;
301 void pa_sconv_s24le_to_float32ne(unsigned n
, const uint8_t *a
, float *b
) {
306 int32_t s
= READ24(a
) << 8;
307 *b
= ((float) s
) / 0x7FFFFFFF;
313 void pa_sconv_s24le_from_float32ne(unsigned n
, const float *a
, uint8_t *b
) {
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);
328 void pa_sconv_s24le_to_float32re(unsigned n
, const uint8_t *a
, float *b
) {
333 int32_t s
= READ24(a
) << 8;
334 float k
= ((float) s
) / 0x7FFFFFFF;
335 *b
= PA_FLOAT32_SWAP(k
);
341 void pa_sconv_s24le_from_float32re(unsigned n
, const float *a
, uint8_t *b
) {
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);
357 void pa_sconv_s24_32le_to_s16ne(unsigned n
, const uint32_t *a
, int16_t *b
) {
362 *b
= (int16_t) (((int32_t) (UINT32_FROM(*a
) << 8)) >> 16);
368 void pa_sconv_s24_32le_to_s16re(unsigned n
, const uint32_t *a
, int16_t *b
) {
373 int16_t s
= (int16_t) ((int32_t) (UINT32_FROM(*a
) << 8) >> 16);
374 *b
= PA_INT16_SWAP(s
);
380 void pa_sconv_s24_32le_from_s16ne(unsigned n
, const int16_t *a
, uint32_t *b
) {
385 *b
= UINT32_TO(((uint32_t) ((int32_t) *a
<< 16)) >> 8);
391 void pa_sconv_s24_32le_from_s16re(unsigned n
, const int16_t *a
, uint32_t *b
) {
396 uint32_t s
= ((uint32_t) ((int32_t) PA_INT16_SWAP(*a
) << 16)) >> 8;
403 void pa_sconv_s24_32le_to_float32ne(unsigned n
, const uint32_t *a
, float *b
) {
408 int32_t s
= (int32_t) (UINT32_FROM(*a
) << 8);
409 *b
= (float) s
/ (float) 0x7FFFFFFF;
415 void pa_sconv_s24_32le_to_float32re(unsigned n
, const uint32_t *a
, float *b
) {
420 int32_t s
= (int32_t) (UINT32_FROM(*a
) << 8);
421 float k
= (float) s
/ (float) 0x7FFFFFFF;
422 *b
= PA_FLOAT32_SWAP(k
);
428 void pa_sconv_s24_32le_from_float32ne(unsigned n
, const float *a
, uint32_t *b
) {
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);
443 void pa_sconv_s24_32le_from_float32re(unsigned n
, const float *a
, uint32_t *b
) {
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);