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 <pulsecore/sconv.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/log.h>
35 #include "endianmacros.h"
37 #include "sconv-s16le.h"
40 #define INT16_FROM PA_INT16_FROM_LE
43 #define UINT16_FROM PA_UINT16_FROM_LE
47 #define INT16_TO PA_INT16_TO_LE
50 #define UINT16_TO PA_UINT16_TO_LE
54 #define INT32_FROM PA_INT32_FROM_LE
57 #define UINT32_FROM PA_UINT32_FROM_LE
61 #define INT32_TO PA_INT32_TO_LE
64 #define UINT32_TO PA_UINT32_TO_LE
68 #define READ24 PA_READ24LE
71 #define WRITE24 PA_WRITE24LE
75 #ifdef WORDS_BIGENDIAN
82 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
89 *(b++) = ((float) INT16_FROM(s))/(float) 0x7FFF;
93 *(b++) = ((float) (*(a++)))/(float) 0x7FFF;
97 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
104 *(b++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
108 *(b++) = (float) (((double) (*(a++)))/0x7FFFFFFF);
112 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
121 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
122 s = (int16_t) lrintf(v * 0x7FFF);
123 *(b++) = INT16_TO(s);
129 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
130 *(b++) = (int16_t) lrintf(v * 0x7FFF);
135 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
144 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
145 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
146 *(b++) = INT32_TO(s);
152 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
153 *(b++) = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
158 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
164 float k = ((float) INT16_FROM(s))/0x7FFF;
165 k = PA_FLOAT32_SWAP(k);
170 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
176 float k = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
177 k = PA_FLOAT32_SWAP(k);
182 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
189 v = PA_FLOAT32_SWAP(v);
190 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
191 s = (int16_t) lrintf(v * 0x7FFF);
192 *(b++) = INT16_TO(s);
196 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
203 v = PA_FLOAT32_SWAP(v);
204 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
205 s = (int32_t) lrint((double) v * 0x7FFFFFFF);
206 *(b++) = INT32_TO(s);
210 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
215 *b = (int16_t) (INT32_FROM(*a) >> 16);
221 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
226 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
227 *b = PA_INT16_SWAP(s);
233 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
238 *b = INT32_TO(((int32_t) *a) << 16);
244 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
249 int32_t s = ((int32_t) PA_INT16_SWAP(*a)) << 16;
256 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
261 *b = (int16_t) (READ24(a) >> 8);
267 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
272 WRITE24(b, ((uint32_t) *a) << 8);
278 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
283 int16_t s = (int16_t) (READ24(a) >> 8);
284 *b = PA_INT16_SWAP(s);
290 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
295 uint32_t s = ((uint32_t) PA_INT16_SWAP(*a)) << 8;
302 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
307 int32_t s = READ24(a) << 8;
308 *b = ((float) s) / 0x7FFFFFFF;
314 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
321 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
322 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
323 WRITE24(b, ((uint32_t) s) >> 8);
329 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
334 int32_t s = READ24(a) << 8;
335 float k = ((float) s) / 0x7FFFFFFF;
336 *b = PA_FLOAT32_SWAP(k);
342 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
349 v = PA_FLOAT32_SWAP(v);
350 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
351 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
352 WRITE24(b, ((uint32_t) s) >> 8);
358 void pa_sconv_s24_32le_to_s16ne(unsigned n, const uint32_t *a, int16_t *b) {
363 *b = (int16_t) (((int32_t) (UINT32_FROM(*a) << 8)) >> 16);
369 void pa_sconv_s24_32le_to_s16re(unsigned n, const uint32_t *a, int16_t *b) {
374 int16_t s = (int16_t) ((int32_t) (UINT32_FROM(*a) << 8) >> 16);
375 *b = PA_INT16_SWAP(s);
381 void pa_sconv_s24_32le_from_s16ne(unsigned n, const int16_t *a, uint32_t *b) {
386 *b = UINT32_TO(((uint32_t) ((int32_t) *a << 16)) >> 8);
392 void pa_sconv_s24_32le_from_s16re(unsigned n, const int16_t *a, uint32_t *b) {
397 uint32_t s = ((uint32_t) ((int32_t) PA_INT16_SWAP(*a) << 16)) >> 8;
404 void pa_sconv_s24_32le_to_float32ne(unsigned n, const uint32_t *a, float *b) {
409 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
410 *b = (float) s / (float) 0x7FFFFFFF;
416 void pa_sconv_s24_32le_to_float32re(unsigned n, const uint32_t *a, float *b) {
421 int32_t s = (int32_t) (UINT32_FROM(*a) << 8);
422 float k = (float) s / (float) 0x7FFFFFFF;
423 *b = PA_FLOAT32_SWAP(k);
429 void pa_sconv_s24_32le_from_float32ne(unsigned n, const float *a, uint32_t *b) {
436 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
437 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
438 *b = UINT32_TO(((uint32_t) s) >> 8);
444 void pa_sconv_s24_32le_from_float32re(unsigned n, const float *a, uint32_t *b) {
451 v = PA_FLOAT32_SWAP(v);
452 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
453 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
454 *b = UINT32_TO(((uint32_t) s) >> 8);