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