]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv-s16le.c
add support for 24bit packed samples
[pulseaudio] / src / pulsecore / sconv-s16le.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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 of the License,
9 or (at your option) any later version.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 /* Despite the name of this file we implement S32 and S24 handling here, too. */
27
28 #include <inttypes.h>
29 #include <stdio.h>
30
31 #include <liboil/liboilfuncs.h>
32
33 #include <pulsecore/sconv.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/log.h>
36
37 #include "endianmacros.h"
38
39 #include "sconv-s16le.h"
40
41 #ifndef INT16_FROM
42 #define INT16_FROM PA_INT16_FROM_LE
43 #endif
44
45 #ifndef INT16_TO
46 #define INT16_TO PA_INT16_TO_LE
47 #endif
48
49 #ifndef INT32_FROM
50 #define INT32_FROM PA_INT32_FROM_LE
51 #endif
52
53 #ifndef INT32_TO
54 #define INT32_TO PA_INT32_TO_LE
55 #endif
56
57 #ifndef READ24
58 #define READ24 PA_READ24LE
59 #endif
60
61 #ifndef WRITE24
62 #define WRITE24 PA_WRITE24LE
63 #endif
64
65 #ifndef SWAP_WORDS
66 #ifdef WORDS_BIGENDIAN
67 #define SWAP_WORDS 1
68 #else
69 #define SWAP_WORDS 0
70 #endif
71 #endif
72
73 void pa_sconv_s16le_to_float32ne(unsigned n, const int16_t *a, float *b) {
74 pa_assert(a);
75 pa_assert(b);
76
77 #if SWAP_WORDS == 1
78
79 for (; n > 0; n--) {
80 int16_t s = *(a++);
81 *(b++) = ((float) INT16_FROM(s))/(float) 0x7FFF;
82 }
83
84 #else
85 {
86 static const double add = 0, factor = 1.0/0x7FFF;
87 oil_scaleconv_f32_s16(b, a, (int) n, &add, &factor);
88 }
89 #endif
90 }
91
92 void pa_sconv_s32le_to_float32ne(unsigned n, const int32_t *a, float *b) {
93 pa_assert(a);
94 pa_assert(b);
95
96 #if SWAP_WORDS == 1
97
98 for (; n > 0; n--) {
99 int32_t s = *(a++);
100 *(b++) = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
101 }
102
103 #else
104 {
105 static const double add = 0, factor = 1.0/0x7FFFFFFF;
106 oil_scaleconv_f32_s32(b, a, (int) n, &add, &factor);
107 }
108 #endif
109 }
110
111 void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, int16_t *b) {
112 pa_assert(a);
113 pa_assert(b);
114
115 #if SWAP_WORDS == 1
116
117 for (; n > 0; n--) {
118 int16_t s;
119 float v = *(a++);
120
121 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.f);
122 s = (int16_t) lrintf(v * 0x7FFF);
123 *(b++) = INT16_TO(s);
124 }
125
126 #else
127 {
128 static const double add = 0, factor = 0x7FFF;
129 oil_scaleconv_s16_f32(b, a, (int) n, &add, &factor);
130 }
131 #endif
132 }
133
134 void pa_sconv_s32le_from_float32ne(unsigned n, const float *a, int32_t *b) {
135 pa_assert(a);
136 pa_assert(b);
137
138 #if SWAP_WORDS == 1
139
140 for (; n > 0; n--) {
141 int32_t s;
142 float v = *(a++);
143
144 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
145 s = (int32_t) lrint((double) v * (double) 0x7FFFFFFF);
146 *(b++) = INT32_TO(s);
147 }
148
149 #else
150 {
151 static const double add = 0, factor = 0x7FFFFFFF;
152 oil_scaleconv_s32_f32(b, a, (int) n, &add, &factor);
153 }
154 #endif
155 }
156
157 void pa_sconv_s16le_to_float32re(unsigned n, const int16_t *a, float *b) {
158 pa_assert(a);
159 pa_assert(b);
160
161 for (; n > 0; n--) {
162 int16_t s = *(a++);
163 float k = ((float) INT16_FROM(s))/0x7FFF;
164 k = PA_FLOAT32_SWAP(k);
165 *(b++) = k;
166 }
167 }
168
169 void pa_sconv_s32le_to_float32re(unsigned n, const int32_t *a, float *b) {
170 pa_assert(a);
171 pa_assert(b);
172
173 for (; n > 0; n--) {
174 int32_t s = *(a++);
175 float k = (float) (((double) INT32_FROM(s))/0x7FFFFFFF);
176 k = PA_FLOAT32_SWAP(k);
177 *(b++) = k;
178 }
179 }
180
181 void pa_sconv_s16le_from_float32re(unsigned n, const float *a, int16_t *b) {
182 pa_assert(a);
183 pa_assert(b);
184
185 for (; n > 0; n--) {
186 int16_t s;
187 float v = *(a++);
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);
192 }
193 }
194
195 void pa_sconv_s32le_from_float32re(unsigned n, const float *a, int32_t *b) {
196 pa_assert(a);
197 pa_assert(b);
198
199 for (; n > 0; n--) {
200 int32_t s;
201 float v = *(a++);
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);
206 }
207 }
208
209 void pa_sconv_s32le_to_s16ne(unsigned n, const int32_t*a, int16_t *b) {
210 pa_assert(a);
211 pa_assert(b);
212
213 for (; n > 0; n--) {
214 *b = (int16_t) (INT32_FROM(*a) >> 16);
215 a++;
216 b++;
217 }
218 }
219
220 void pa_sconv_s32le_to_s16re(unsigned n, const int32_t*a, int16_t *b) {
221 pa_assert(a);
222 pa_assert(b);
223
224 for (; n > 0; n--) {
225 int16_t s = (int16_t) (INT32_FROM(*a) >> 16);
226 *b = PA_INT16_SWAP(s);
227 a++;
228 b++;
229 }
230 }
231
232 void pa_sconv_s32le_from_s16ne(unsigned n, const int16_t *a, int32_t *b) {
233 pa_assert(a);
234 pa_assert(b);
235
236 for (; n > 0; n--) {
237 *b = INT32_TO(((int32_t) *a) << 16);
238 a++;
239 b++;
240 }
241 }
242
243 void pa_sconv_s32le_from_s16re(unsigned n, const int16_t *a, int32_t *b) {
244 pa_assert(a);
245 pa_assert(b);
246
247 for (; n > 0; n--) {
248 int32_t s = ((int32_t) PA_INT16_SWAP(*a)) << 16;
249 *b = INT32_TO(s);
250 a++;
251 b++;
252 }
253 }
254
255 void pa_sconv_s24le_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
256 pa_assert(a);
257 pa_assert(b);
258
259 for (; n > 0; n--) {
260 *b = (int16_t) (READ24(a) >> 8);
261 a += 3;
262 b++;
263 }
264 }
265
266 void pa_sconv_s24le_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
267 pa_assert(a);
268 pa_assert(b);
269
270 for (; n > 0; n--) {
271 WRITE24(b, ((uint32_t) *a) << 8);
272 a++;
273 b += 3;
274 }
275 }
276
277 void pa_sconv_s24le_to_s16re(unsigned n, const uint8_t *a, int16_t *b) {
278 pa_assert(a);
279 pa_assert(b);
280
281 for (; n > 0; n--) {
282 int16_t s = (int16_t) (READ24(a) >> 8);
283 *b = PA_INT16_SWAP(s);
284 a += 3;
285 b++;
286 }
287 }
288
289 void pa_sconv_s24le_from_s16re(unsigned n, const int16_t *a, uint8_t *b) {
290 pa_assert(a);
291 pa_assert(b);
292
293 for (; n > 0; n--) {
294 uint32_t s = ((uint32_t) PA_INT16_SWAP(*a)) << 8;
295 WRITE24(b, s);
296 a++;
297 b += 3;
298 }
299 }
300
301 void pa_sconv_s24le_to_float32ne(unsigned n, const uint8_t *a, float *b) {
302 pa_assert(a);
303 pa_assert(b);
304
305 for (; n > 0; n--) {
306 int32_t s = READ24(a) << 8;
307 *b = ((float) s) / 0x7FFFFFFF;
308 a += 3;
309 b ++;
310 }
311 }
312
313 void pa_sconv_s24le_from_float32ne(unsigned n, const float *a, uint8_t *b) {
314 pa_assert(a);
315 pa_assert(b);
316
317 for (; n > 0; n--) {
318 int32_t s;
319 float v = *a;
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);
323 a++;
324 b+=3;
325 }
326 }
327
328 void pa_sconv_s24le_to_float32re(unsigned n, const uint8_t *a, float *b) {
329 pa_assert(a);
330 pa_assert(b);
331
332 for (; n > 0; n--) {
333 int32_t s = READ24(a) << 8;
334 float k = ((float) s) / 0x7FFFFFFF;
335 *b = PA_FLOAT32_SWAP(k);
336 a += 3;
337 b ++;
338 }
339 }
340
341 void pa_sconv_s24le_from_float32re(unsigned n, const float *a, uint8_t *b) {
342 pa_assert(a);
343 pa_assert(b);
344
345 for (; n > 0; n--) {
346 int32_t s;
347 float v = *a;
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);
352 a++;
353 b+=3;
354 }
355 }