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. */
31 #include <liboil/liboilfuncs.h>
33 #include <pulsecore/sconv.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/log.h>
37 #include "endianmacros.h"
39 #include "sconv-s16le.h"
42 #define INT16_FROM PA_INT16_FROM_LE
45 #define UINT16_FROM PA_UINT16_FROM_LE
49 #define INT16_TO PA_INT16_TO_LE
52 #define UINT16_TO PA_UINT16_TO_LE
56 #define INT32_FROM PA_INT32_FROM_LE
59 #define UINT32_FROM PA_UINT32_FROM_LE
63 #define INT32_TO PA_INT32_TO_LE
66 #define UINT32_TO PA_UINT32_TO_LE
70 #define READ24 PA_READ24LE
73 #define WRITE24 PA_WRITE24LE
77 #ifdef WORDS_BIGENDIAN
84 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
92 *(b++) = ((float) INT16_FROM(s))/(float) 0x7FFF;
97 static const double add = 0, factor = 1.0/0x7FFF;
98 oil_scaleconv_f32_s16(b, a, (int) n, &add, &factor);
103 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
111 *(b++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
116 static const double add = 0, factor = 1.0/0x7FFFFFFF;
117 oil_scaleconv_f32_s32(b, a, (int) n, &add, &factor);
122 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
132 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
133 s = (int16_t) lrintf(v * 0x7FFF);
134 *(b++) = INT16_TO(s);
139 static const double add = 0, factor = 0x7FFF;
140 oil_scaleconv_s16_f32(b, a, (int) n, &add, &factor);
145 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
155 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
156 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
157 *(b++) = INT32_TO(s);
162 static const double add = 0, factor = 0x7FFFFFFF;
163 oil_scaleconv_s32_f32(b, a, (int) n, &add, &factor);
168 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
174 float k = ((float) INT16_FROM(s))/0x7FFF;
175 k = PA_FLOAT32_SWAP(k);
180 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
186 float k = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
187 k = PA_FLOAT32_SWAP(k);
192 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
199 v = PA_FLOAT32_SWAP(v);
200 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
201 s = (int16_t) lrintf(v * 0x7FFF);
202 *(b++) = INT16_TO(s);
206 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
213 v = PA_FLOAT32_SWAP(v);
214 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
215 s = (int32_t) lrint((double) v * 0x7FFFFFFF);
216 *(b++) = INT32_TO(s);
220 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
225 *b = (int16_t) (INT32_FROM(*a) >> 16);
231 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
236 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
237 *b = PA_INT16_SWAP(s);
243 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
248 *b = INT32_TO(((int32_t) *a) << 16);
254 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
259 int32_t s = ((int32_t) PA_INT16_SWAP(*a)) << 16;
266 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
271 *b = (int16_t) (READ24(a) >> 8);
277 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
282 WRITE24(b, ((uint32_t) *a) << 8);
288 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
293 int16_t s = (int16_t) (READ24(a) >> 8);
294 *b = PA_INT16_SWAP(s);
300 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
305 uint32_t s = ((uint32_t) PA_INT16_SWAP(*a)) << 8;
312 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
317 int32_t s = READ24(a) << 8;
318 *b = ((float) s) / 0x7FFFFFFF;
324 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
331 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
332 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
333 WRITE24(b, ((uint32_t) s) >> 8);
339 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
344 int32_t s = READ24(a) << 8;
345 float k = ((float) s) / 0x7FFFFFFF;
346 *b = PA_FLOAT32_SWAP(k);
352 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
359 v = PA_FLOAT32_SWAP(v);
360 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
361 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
362 WRITE24(b, ((uint32_t) s) >> 8);
368 void pa_sconv_s24_32le_to_s16ne(unsigned n, const uint32_t *a, int16_t *b) {
373 *b = (int16_t) (((int32_t) (UINT32_FROM(*a) << 8)) >> 16);
379 void pa_sconv_s24_32le_to_s16re(unsigned n, const uint32_t *a, int16_t *b) {
384 int16_t s = (int16_t) ((int32_t) (UINT32_FROM(*a) << 8) >> 16);
385 *b = PA_INT16_SWAP(s);
391 void pa_sconv_s24_32le_from_s16ne(unsigned n, const int16_t *a, uint32_t *b) {
396 *b = UINT32_TO(((uint32_t) ((int32_t) *a << 16)) >> 8);
402 void pa_sconv_s24_32le_from_s16re(unsigned n, const int16_t *a, uint32_t *b) {
407 uint32_t s = ((uint32_t) ((int32_t) PA_INT16_SWAP(*a) << 16)) >> 8;
414 void pa_sconv_s24_32le_to_float32ne(unsigned n, const uint32_t *a, float *b) {
419 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
420 *b = (float) s / (float) 0x7FFFFFFF;
426 void pa_sconv_s24_32le_to_float32re(unsigned n, const uint32_t *a, float *b) {
431 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
432 float k = (float) s / (float) 0x7FFFFFFF;
433 *b = PA_FLOAT32_SWAP(k);
439 void pa_sconv_s24_32le_from_float32ne(unsigned n, const float *a, uint32_t *b) {
446 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
447 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
448 *b = UINT32_TO(((uint32_t) s) >> 8);
454 void pa_sconv_s24_32le_from_float32re(unsigned n, const float *a, uint32_t *b) {
461 v = PA_FLOAT32_SWAP(v);
462 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
463 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
464 *b = UINT32_TO(((uint32_t) s) >> 8);