]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
use a free list for allocating reply_info structs
[pulseaudio] / src / pulsecore / sconv-s16le.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 /* Despite the name of this file we implement S32 handling here, too. */
29
30 #include <inttypes.h>
31 #include <stdio.h>
32
33 #include <liboil/liboilfuncs.h>
34
35 #include <pulsecore/sconv.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/log.h>
38
39 #include "endianmacros.h"
40
41 #include "sconv-s16le.h"
42
43 #ifndef INT16_FROM
44 #define INT16_FROM PA_INT16_FROM_LE
45 #endif
46
47 #ifndef INT16_TO
48 #define INT16_TO PA_INT16_TO_LE
49 #endif
50
51 #ifndef INT32_FROM
52 #define INT32_FROM PA_INT32_FROM_LE
53 #endif
54
55 #ifndef INT32_TO
56 #define INT32_TO PA_INT32_TO_LE
57 #endif
58
59 #ifndef SWAP_WORDS
60 #ifdef WORDS_BIGENDIAN
61 #define SWAP_WORDS 1
62 #else
63 #define SWAP_WORDS 0
64 #endif
65 #endif
66
67 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
68 pa_assert(a);
69 pa_assert(b);
70
71 #if SWAP_WORDS == 1
72
73 for (; n > 0; n--) {
74 int16_t s = *(a++);
75 *(b++) = ((float) INT16_FROM(s))/0x7FFF;
76 }
77
78 #else
79 {
80 static const double add = 0, factor = 1.0/0x7FFF;
81 oil_scaleconv_f32_s16(b, a, n, &add, &factor);
82 }
83 #endif
84 }
85
86 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
87 pa_assert(a);
88 pa_assert(b);
89
90 #if SWAP_WORDS == 1
91
92 for (; n > 0; n--) {
93 int32_t s = *(a++);
94 *(b++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
95 }
96
97 #else
98 {
99 static const double add = 0, factor = 1.0/0x7FFFFFFF;
100 oil_scaleconv_f32_s32(b, a, n, &add, &factor);
101 }
102 #endif
103 }
104
105 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
106 pa_assert(a);
107 pa_assert(b);
108
109 #if SWAP_WORDS == 1
110
111 for (; n > 0; n--) {
112 int16_t s;
113 float v = *(a++);
114
115 v = PA_CLAMP_UNLIKELY(v, -1, 1);
116 s = (int16_t) (v * 0x7FFF);
117 *(b++) = INT16_TO(s);
118 }
119
120 #else
121 {
122 static const double add = 0, factor = 0x7FFF;
123 oil_scaleconv_s16_f32(b, a, n, &add, &factor);
124 }
125 #endif
126 }
127
128 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
129 pa_assert(a);
130 pa_assert(b);
131
132 #if SWAP_WORDS == 1
133
134 for (; n > 0; n--) {
135 int32_t s;
136 float v = *(a++);
137
138 v = PA_CLAMP_UNLIKELY(v, -1, 1);
139 s = (int32_t) ((double) v * (double) 0x7FFFFFFF);
140 *(b++) = INT32_TO(s);
141 }
142
143 #else
144 {
145 static const double add = 0, factor = 0x7FFFFFFF;
146 oil_scaleconv_s32_f32(b, a, n, &add, &factor);
147 }
148 #endif
149 }
150
151 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
152 pa_assert(a);
153 pa_assert(b);
154
155 for (; n > 0; n--) {
156 int16_t s = *(a++);
157 float k = ((float) INT16_FROM(s))/0x7FFF;
158 uint32_t *j = (uint32_t*) &k;
159 *j = PA_UINT32_SWAP(*j);
160 *(b++) = k;
161 }
162 }
163
164 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
165 pa_assert(a);
166 pa_assert(b);
167
168 for (; n > 0; n--) {
169 int32_t s = *(a++);
170 float k = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
171 uint32_t *j = (uint32_t*) &k;
172 *j = PA_UINT32_SWAP(*j);
173 *(b++) = k;
174 }
175 }
176
177 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
178 pa_assert(a);
179 pa_assert(b);
180
181 for (; n > 0; n--) {
182 int16_t s;
183 float v = *(a++);
184 uint32_t *j = (uint32_t*) &v;
185 *j = PA_UINT32_SWAP(*j);
186 v = PA_CLAMP_UNLIKELY(v, -1, 1);
187 s = (int16_t) (v * 0x7FFF);
188 *(b++) = INT16_TO(s);
189 }
190 }
191
192 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
193 pa_assert(a);
194 pa_assert(b);
195
196 for (; n > 0; n--) {
197 int32_t s;
198 float v = *(a++);
199 uint32_t *j = (uint32_t*) &v;
200 *j = PA_UINT32_SWAP(*j);
201 v = PA_CLAMP_UNLIKELY(v, -1, 1);
202 s = (int32_t) ((double) v * 0x7FFFFFFF);
203 *(b++) = INT32_TO(s);
204 }
205 }
206
207 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
208 pa_assert(a);
209 pa_assert(b);
210
211 for (; n > 0; n--) {
212 *b = (int16_t) (INT32_FROM(*a) >> 16);
213 a++;
214 b++;
215 }
216 }
217
218 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
219 pa_assert(a);
220 pa_assert(b);
221
222 for (; n > 0; n--) {
223 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
224 *b = PA_UINT32_SWAP(s);
225 a++;
226 b++;
227 }
228 }
229
230 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
231 pa_assert(a);
232 pa_assert(b);
233
234 for (; n > 0; n--) {
235 *b = INT32_TO(((int32_t) *a) << 16);
236 a++;
237 b++;
238 }
239 }
240
241 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
242 pa_assert(a);
243 pa_assert(b);
244
245 for (; n > 0; n--) {
246 int32_t s = ((int32_t) PA_UINT16_SWAP(*a)) << 16;
247 *b = INT32_TO(s);
248 a++;
249 b++;
250 }
251 }