]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[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 #include <math.h>
30
31 #include <pulsecore/g711.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/endianmacros.h>
34
35 #include <pulsecore/sconv-s16le.h>
36 #include <pulsecore/sconv-s16be.h>
37
38 #include "sconv.h"
39
40 /* u8 */
41 static void u8_to_float32ne(unsigned n, const uint8_t *a, float *b) {
42 pa_assert(a);
43 pa_assert(b);
44
45 for (; n > 0; n--, a++, b++)
46 *b = (*a * 1.0/128.0) - 1.0;
47 }
48
49 static void u8_from_float32ne(unsigned n, const float *a, uint8_t *b) {
50 pa_assert(a);
51 pa_assert(b);
52
53 for (; n > 0; n--, a++, b++) {
54 float v;
55 v = (*a * 127.0) + 128.0;
56 v = PA_CLAMP_UNLIKELY (v, 0.0, 255.0);
57 *b = rint (v);
58 }
59 }
60
61 static void u8_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
62 pa_assert(a);
63 pa_assert(b);
64
65 for (; n > 0; n--, a++, b++)
66 *b = (((int16_t)*a) - 128) << 8;
67 }
68
69 static void u8_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
70
71 pa_assert(a);
72 pa_assert(b);
73
74 for (; n > 0; n--, a++, b++)
75 *b = (uint8_t) ((uint16_t) *a >> 8) + (uint8_t) 0x80U;
76 }
77
78 /* float32 */
79
80 static void float32ne_to_float32ne(unsigned n, const float *a, float *b) {
81 pa_assert(a);
82 pa_assert(b);
83
84 memcpy(b, a, (int) (sizeof(float) * n));
85 }
86
87 static void float32re_to_float32ne(unsigned n, const float *a, float *b) {
88 pa_assert(a);
89 pa_assert(b);
90
91 for (; n > 0; n--, a++, b++)
92 *((uint32_t *) b) = PA_UINT32_SWAP(*((uint32_t *) a));
93 }
94
95 /* s16 */
96
97 static void s16ne_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
98 pa_assert(a);
99 pa_assert(b);
100
101 memcpy(b, a, (int) (sizeof(int16_t) * n));
102 }
103
104 static void s16re_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
105 pa_assert(a);
106 pa_assert(b);
107
108 for (; n > 0; n--, a++, b++)
109 *b = PA_INT16_SWAP(*a);
110 }
111
112 /* ulaw */
113
114 static void ulaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
115 pa_assert(a);
116 pa_assert(b);
117
118 for (; n > 0; n--)
119 *(b++) = (float) st_ulaw2linear16(*(a++)) / 0x8000;
120 }
121
122 static void ulaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
123 pa_assert(a);
124 pa_assert(b);
125
126 for (; n > 0; n--) {
127 float v = *(a++);
128 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
129 v *= 0x1FFF;
130 *(b++) = st_14linear2ulaw((int16_t) lrintf(v));
131 }
132 }
133
134 static void ulaw_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
135 pa_assert(a);
136 pa_assert(b);
137
138 for (; n > 0; n--, a++, b++)
139 *b = st_ulaw2linear16(*a);
140 }
141
142 static void ulaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
143 pa_assert(a);
144 pa_assert(b);
145
146 for (; n > 0; n--, a++, b++)
147 *b = st_14linear2ulaw(*a >> 2);
148 }
149
150 /* alaw */
151
152 static void alaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
153 pa_assert(a);
154 pa_assert(b);
155
156 for (; n > 0; n--, a++, b++)
157 *b = (float) st_alaw2linear16(*a) / 0x8000;
158 }
159
160 static void alaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
161 pa_assert(a);
162 pa_assert(b);
163
164 for (; n > 0; n--, a++, b++) {
165 float v = *a;
166 v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
167 v *= 0xFFF;
168 *b = st_13linear2alaw((int16_t) lrintf(v));
169 }
170 }
171
172 static void alaw_to_s16ne(unsigned n, const int8_t *a, int16_t *b) {
173 pa_assert(a);
174 pa_assert(b);
175
176 for (; n > 0; n--, a++, b++)
177 *b = st_alaw2linear16((uint8_t) *a);
178 }
179
180 static void alaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
181 pa_assert(a);
182 pa_assert(b);
183
184 for (; n > 0; n--, a++, b++)
185 *b = st_13linear2alaw(*a >> 3);
186 }
187
188 static pa_convert_func_t to_float32ne_table[] = {
189 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_float32ne,
190 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_float32ne,
191 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_float32ne,
192 [PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_to_float32ne,
193 [PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_to_float32ne,
194 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_float32ne,
195 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_float32ne,
196 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_float32ne,
197 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_float32ne,
198 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_float32ne,
199 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_float32ne,
200 [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
201 [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
202 };
203
204 pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
205 pa_assert(pa_sample_format_valid(f));
206
207 return to_float32ne_table[f];
208 }
209
210 void pa_set_convert_to_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
211 pa_assert(pa_sample_format_valid(f));
212
213 to_float32ne_table[f] = func;
214 }
215
216 static pa_convert_func_t from_float32ne_table[] = {
217 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_float32ne,
218 [PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_from_float32ne,
219 [PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_from_float32ne,
220 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_float32ne,
221 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_float32ne,
222 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_float32ne,
223 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_float32ne,
224 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_float32ne,
225 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_float32ne,
226 [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
227 [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
228 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_float32ne,
229 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_float32ne
230 };
231
232 pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
233 pa_assert(pa_sample_format_valid(f));
234
235 return from_float32ne_table[f];
236 }
237
238 void pa_set_convert_from_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
239 pa_assert(pa_sample_format_valid(f));
240
241 from_float32ne_table[f] = func;
242 }
243
244 static pa_convert_func_t to_s16ne_table[] = {
245 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_s16ne,
246 [PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
247 [PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
248 [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_to_s16ne,
249 [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_to_s16ne,
250 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_s16ne,
251 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_s16ne,
252 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_s16ne,
253 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_s16ne,
254 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_s16ne,
255 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_s16ne,
256 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_s16ne,
257 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_s16ne
258 };
259
260 pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
261 pa_assert(pa_sample_format_valid(f));
262
263 return to_s16ne_table[f];
264 }
265
266 void pa_set_convert_to_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
267 pa_assert(pa_sample_format_valid(f));
268
269 to_s16ne_table[f] = func;
270 }
271
272 static pa_convert_func_t from_s16ne_table[] = {
273 [PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_s16ne,
274 [PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
275 [PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
276 [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_from_s16ne,
277 [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_from_s16ne,
278 [PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_s16ne,
279 [PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_s16ne,
280 [PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_s16ne,
281 [PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_s16ne,
282 [PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_s16ne,
283 [PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_s16ne,
284 [PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_s16ne,
285 [PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_s16ne,
286 };
287
288 pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
289 pa_assert(pa_sample_format_valid(f));
290
291 return from_s16ne_table[f];
292 }
293
294 void pa_set_convert_from_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
295 pa_assert(pa_sample_format_valid(f));
296
297 from_s16ne_table[f] = func;
298 }