]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv.c
Get rid of liboil
[pulseaudio] / src / pulsecore / sconv.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <pulsecore/g711.h>
31 #include <pulsecore/macro.h>
32
33 #include "endianmacros.h"
34 #include "sconv-s16le.h"
35 #include "sconv-s16be.h"
36
37 #include "sconv.h"
38
39 /* u8 */
40 static void u8_to_float32ne(unsigned n, const uint8_t *a, float *b) {
41 pa_assert(a);
42 pa_assert(b);
43
44 for (; n > 0; n--, a++, b++)
45 *b = (*a * 1.0/128.0) - 1.0;
46 }
47
48 static void u8_from_float32ne(unsigned n, const float *a, uint8_t *b) {
49 pa_assert(a);
50 pa_assert(b);
51
52 for (; n > 0; n--, a++, b++) {
53 float v;
54 v = (*a * 127.0) + 128.0;
55 v = PA_CLAMP_UNLIKELY (v, 0.0, 255.0);
56 *b = rint (v);
57 }
58 }
59
60 static void u8_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
61 pa_assert(a);
62 pa_assert(b);
63
64 for (; n > 0; n--, a++, b++)
65 *b = (((int16_t)*a) - 128) << 8;
66 }
67
68 static void u8_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
69
70 pa_assert(a);
71 pa_assert(b);
72
73 for (; n > 0; n--, a++, b++)
74 *b = (uint8_t) ((uint16_t) *a >> 8) + (uint8_t) 0x80U;
75 }
76
77 /* float32 */
78
79 static void float32ne_to_float32ne(unsigned n, const float *a, float *b) {
80 pa_assert(a);
81 pa_assert(b);
82
83 memcpy(b, a, (int) (sizeof(float) * n));
84 }
85
86 static void float32re_to_float32ne(unsigned n, const float *a, float *b) {
87 pa_assert(a);
88 pa_assert(b);
89
90 for (; n > 0; n--, a++, b++)
91 *((uint32_t *) b) = PA_UINT32_SWAP(*((uint32_t *) a));
92 }
93
94 /* s16 */
95
96 static void s16ne_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
97 pa_assert(a);
98 pa_assert(b);
99
100 memcpy(b, a, (int) (sizeof(int16_t) * n));
101 }
102
103 static void s16re_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
104 pa_assert(a);
105 pa_assert(b);
106
107 for (; n > 0; n--, a++, b++)
108 *b = PA_INT16_SWAP(*a);
109 }
110
111 /* ulaw */
112
113 static void ulaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
114 pa_assert(a);
115 pa_assert(b);
116
117 for (; n > 0; n--)
118 *(b++) = (float) st_ulaw2linear16(*(a++)) / 0x8000;
119 }
120
121 static void ulaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
122 pa_assert(a);
123 pa_assert(b);
124
125 for (; n > 0; n--) {
126 float v = *(a++);
127 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
128 v *= 0x1FFF;
129 *(b++) = st_14linear2ulaw((int16_t) lrintf(v));
130 }
131 }
132
133 static void ulaw_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
134 pa_assert(a);
135 pa_assert(b);
136
137 for (; n > 0; n--, a++, b++)
138 *b = st_ulaw2linear16(*a);
139 }
140
141 static void ulaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
142 pa_assert(a);
143 pa_assert(b);
144
145 for (; n > 0; n--, a++, b++)
146 *b = st_14linear2ulaw(*a >> 2);
147 }
148
149 /* alaw */
150
151 static void alaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
152 pa_assert(a);
153 pa_assert(b);
154
155 for (; n > 0; n--, a++, b++)
156 *b = (float) st_alaw2linear16(*a) / 0x8000;
157 }
158
159 static void alaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
160 pa_assert(a);
161 pa_assert(b);
162
163 for (; n > 0; n--, a++, b++) {
164 float v = *a;
165 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
166 v *= 0xFFF;
167 *b = st_13linear2alaw((int16_t) lrintf(v));
168 }
169 }
170
171 static void alaw_to_s16ne(unsigned n, const int8_t *a, int16_t *b) {
172 pa_assert(a);
173 pa_assert(b);
174
175 for (; n > 0; n--, a++, b++)
176 *b = st_alaw2linear16((uint8_t) *a);
177 }
178
179 static void alaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
180 pa_assert(a);
181 pa_assert(b);
182
183 for (; n > 0; n--, a++, b++)
184 *b = st_13linear2alaw(*a >> 3);
185 }
186
187 pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
188
189 static const pa_convert_func_t table[] = {
190 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_float32ne,
191 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_float32ne,
192 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_float32ne,
193 [PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_to_float32ne,
194 [PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_to_float32ne,
195 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_float32ne,
196 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_float32ne,
197 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_float32ne,
198 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_float32ne,
199 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_float32ne,
200 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_float32ne,
201 [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
202 [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
203 };
204
205 pa_assert(f >= 0);
206 pa_assert(f < PA_SAMPLE_MAX);
207
208 return table[f];
209 }
210
211 pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
212
213 static const pa_convert_func_t table[] = {
214 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_float32ne,
215 [PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_from_float32ne,
216 [PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_from_float32ne,
217 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_float32ne,
218 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_float32ne,
219 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_float32ne,
220 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_float32ne,
221 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_float32ne,
222 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_float32ne,
223 [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
224 [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
225 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_float32ne,
226 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_float32ne
227 };
228
229 pa_assert(f >= 0);
230 pa_assert(f < PA_SAMPLE_MAX);
231
232 return table[f];
233 }
234
235 pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
236
237 static const pa_convert_func_t table[] = {
238 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_s16ne,
239 [PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
240 [PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
241 [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_to_s16ne,
242 [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_to_s16ne,
243 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_s16ne,
244 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_s16ne,
245 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_s16ne,
246 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_s16ne,
247 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_s16ne,
248 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_s16ne,
249 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_s16ne,
250 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_s16ne
251 };
252
253 pa_assert(f >= 0);
254 pa_assert(f < PA_SAMPLE_MAX);
255
256 return table[f];
257 }
258
259 pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
260
261 static const pa_convert_func_t table[] = {
262 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_s16ne,
263 [PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
264 [PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
265 [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_from_s16ne,
266 [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_from_s16ne,
267 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_s16ne,
268 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_s16ne,
269 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_s16ne,
270 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_s16ne,
271 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_s16ne,
272 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_s16ne,
273 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_s16ne,
274 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_s16ne,
275 };
276
277 pa_assert(f >= 0);
278 pa_assert(f < PA_SAMPLE_MAX);
279
280 return table[f];
281 }