]>
code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
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
++) = INT16_FROM(s
) * (1.0f
/ (1 << 15));
92 *(b
++) = *(a
++) * (1.0f
/ (1 << 15));
96 void pa_sconv_s32le_to_float32ne(unsigned n
, const int32_t *a
, float *b
) {
103 *(b
++) = INT32_FROM(s
) * (1.0f
/ (1U << 31));
107 *(b
++) = *(a
++) * (1.0f
/ (1U << 31));
111 void pa_sconv_s16le_from_float32ne(unsigned n
, const float *a
, int16_t *b
) {
118 float v
= *(a
++) * (1 << 15);
120 s
= (int16_t) PA_CLAMP_UNLIKELY(lrintf(v
), -0x8000, 0x7FFF);
121 *(b
++) = INT16_TO(s
);
125 float v
= *(a
++) * (1 << 15);
127 *(b
++) = (int16_t) PA_CLAMP_UNLIKELY(lrintf(v
), -0x8000, 0x7FFF);
132 void pa_sconv_s32le_from_float32ne(unsigned n
, const float *a
, int32_t *b
) {
139 float v
= *(a
++) * (1U << 31);
141 s
= (int32_t) PA_CLAMP_UNLIKELY(llrintf(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
142 *(b
++) = INT32_TO(s
);
146 float v
= *(a
++) * (1U << 31);
148 *(b
++) = (int32_t) PA_CLAMP_UNLIKELY(llrintf(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
153 void pa_sconv_s16le_to_float32re(unsigned n
, const int16_t *a
, float *b
) {
159 float k
= INT16_FROM(s
) * (1.0f
/ (1 << 15));
160 k
= PA_FLOAT32_SWAP(k
);
165 void pa_sconv_s32le_to_float32re(unsigned n
, const int32_t *a
, float *b
) {
171 float k
= INT32_FROM(s
) * (1.0f
/ (1U << 31));
172 k
= PA_FLOAT32_SWAP(k
);
177 void pa_sconv_s16le_from_float32re(unsigned n
, const float *a
, int16_t *b
) {
184 v
= PA_FLOAT32_SWAP(v
) * (1 << 15);
185 s
= (int16_t) PA_CLAMP_UNLIKELY(lrintf(v
), -0x8000, 0x7FFF);
186 *(b
++) = INT16_TO(s
);
190 void pa_sconv_s32le_from_float32re(unsigned n
, const float *a
, int32_t *b
) {
197 v
= PA_FLOAT32_SWAP(v
) * (1U << 31);
198 s
= (int32_t) PA_CLAMP_UNLIKELY(llrintf(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
199 *(b
++) = INT32_TO(s
);
203 void pa_sconv_s32le_to_s16ne(unsigned n
, const int32_t*a
, int16_t *b
) {
208 *b
= (int16_t) (INT32_FROM(*a
) >> 16);
214 void pa_sconv_s32le_to_s16re(unsigned n
, const int32_t*a
, int16_t *b
) {
219 int16_t s
= (int16_t) (INT32_FROM(*a
) >> 16);
220 *b
= PA_INT16_SWAP(s
);
226 void pa_sconv_s32le_from_s16ne(unsigned n
, const int16_t *a
, int32_t *b
) {
231 *b
= INT32_TO(((int32_t) *a
) << 16);
237 void pa_sconv_s32le_from_s16re(unsigned n
, const int16_t *a
, int32_t *b
) {
242 int32_t s
= ((int32_t) PA_INT16_SWAP(*a
)) << 16;
249 void pa_sconv_s24le_to_s16ne(unsigned n
, const uint8_t *a
, int16_t *b
) {
254 *b
= (int16_t) (READ24(a
) >> 8);
260 void pa_sconv_s24le_from_s16ne(unsigned n
, const int16_t *a
, uint8_t *b
) {
265 WRITE24(b
, ((uint32_t) *a
) << 8);
271 void pa_sconv_s24le_to_s16re(unsigned n
, const uint8_t *a
, int16_t *b
) {
276 int16_t s
= (int16_t) (READ24(a
) >> 8);
277 *b
= PA_INT16_SWAP(s
);
283 void pa_sconv_s24le_from_s16re(unsigned n
, const int16_t *a
, uint8_t *b
) {
288 uint32_t s
= ((uint32_t) PA_INT16_SWAP(*a
)) << 8;
295 void pa_sconv_s24le_to_float32ne(unsigned n
, const uint8_t *a
, float *b
) {
300 int32_t s
= READ24(a
) << 8;
301 *b
= s
* (1.0f
/ (1U << 31));
307 void pa_sconv_s24le_from_float32ne(unsigned n
, const float *a
, uint8_t *b
) {
313 float v
= *a
* (1U << 31);
314 s
= (int32_t) PA_CLAMP_UNLIKELY(llrint(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
315 WRITE24(b
, ((uint32_t) s
) >> 8);
321 void pa_sconv_s24le_to_float32re(unsigned n
, const uint8_t *a
, float *b
) {
326 int32_t s
= READ24(a
) << 8;
327 float k
= s
* (1.0f
/ (1U << 31));
328 *b
= PA_FLOAT32_SWAP(k
);
334 void pa_sconv_s24le_from_float32re(unsigned n
, const float *a
, uint8_t *b
) {
341 v
= PA_FLOAT32_SWAP(v
) * (1U << 31);
342 s
= (int32_t) PA_CLAMP_UNLIKELY(llrint(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
343 WRITE24(b
, ((uint32_t) s
) >> 8);
349 void pa_sconv_s24_32le_to_s16ne(unsigned n
, const uint32_t *a
, int16_t *b
) {
354 *b
= (int16_t) (((int32_t) (UINT32_FROM(*a
) << 8)) >> 16);
360 void pa_sconv_s24_32le_to_s16re(unsigned n
, const uint32_t *a
, int16_t *b
) {
365 int16_t s
= (int16_t) ((int32_t) (UINT32_FROM(*a
) << 8) >> 16);
366 *b
= PA_INT16_SWAP(s
);
372 void pa_sconv_s24_32le_from_s16ne(unsigned n
, const int16_t *a
, uint32_t *b
) {
377 *b
= UINT32_TO(((uint32_t) ((int32_t) *a
<< 16)) >> 8);
383 void pa_sconv_s24_32le_from_s16re(unsigned n
, const int16_t *a
, uint32_t *b
) {
388 uint32_t s
= ((uint32_t) ((int32_t) PA_INT16_SWAP(*a
) << 16)) >> 8;
395 void pa_sconv_s24_32le_to_float32ne(unsigned n
, const uint32_t *a
, float *b
) {
400 int32_t s
= (int32_t) (UINT32_FROM(*a
) << 8);
401 *b
= s
* (1.0f
/ (1U << 31));
407 void pa_sconv_s24_32le_to_float32re(unsigned n
, const uint32_t *a
, float *b
) {
412 int32_t s
= (int32_t) (UINT32_FROM(*a
) << 8);
413 float k
= s
* (1.0f
/ (1U << 31));
414 *b
= PA_FLOAT32_SWAP(k
);
420 void pa_sconv_s24_32le_from_float32ne(unsigned n
, const float *a
, uint32_t *b
) {
426 float v
= *a
* (1U << 31);
427 s
= (int32_t) PA_CLAMP_UNLIKELY(llrint(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
428 *b
= UINT32_TO(((uint32_t) s
) >> 8);
434 void pa_sconv_s24_32le_from_float32re(unsigned n
, const float *a
, uint32_t *b
) {
441 v
= PA_FLOAT32_SWAP(v
) * (1U << 31);
442 s
= (int32_t) PA_CLAMP_UNLIKELY(llrint(v
), -0x80000000LL
, 0x7FFFFFFFLL
);
443 *b
= UINT32_TO(((uint32_t) s
) >> 8);