Git init
[framework/multimedia/pulseaudio.git] / 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 static pa_convert_func_t to_float32ne_table[] = {
188     [PA_SAMPLE_U8]        = (pa_convert_func_t) u8_to_float32ne,
189     [PA_SAMPLE_ALAW]      = (pa_convert_func_t) alaw_to_float32ne,
190     [PA_SAMPLE_ULAW]      = (pa_convert_func_t) ulaw_to_float32ne,
191     [PA_SAMPLE_S16LE]     = (pa_convert_func_t) pa_sconv_s16le_to_float32ne,
192     [PA_SAMPLE_S16BE]     = (pa_convert_func_t) pa_sconv_s16be_to_float32ne,
193     [PA_SAMPLE_S32LE]     = (pa_convert_func_t) pa_sconv_s32le_to_float32ne,
194     [PA_SAMPLE_S32BE]     = (pa_convert_func_t) pa_sconv_s32be_to_float32ne,
195     [PA_SAMPLE_S24LE]     = (pa_convert_func_t) pa_sconv_s24le_to_float32ne,
196     [PA_SAMPLE_S24BE]     = (pa_convert_func_t) pa_sconv_s24be_to_float32ne,
197     [PA_SAMPLE_S24_32LE]  = (pa_convert_func_t) pa_sconv_s24_32le_to_float32ne,
198     [PA_SAMPLE_S24_32BE]  = (pa_convert_func_t) pa_sconv_s24_32be_to_float32ne,
199     [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
200     [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
201 };
202
203 pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
204
205     pa_assert(f >= 0);
206     pa_assert(f < PA_SAMPLE_MAX);
207
208     return to_float32ne_table[f];
209 }
210
211 void pa_set_convert_to_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
212
213     pa_assert(f >= 0);
214     pa_assert(f < PA_SAMPLE_MAX);
215
216     to_float32ne_table[f] = func;
217 }
218
219 static pa_convert_func_t from_float32ne_table[] = {
220     [PA_SAMPLE_U8]        = (pa_convert_func_t) u8_from_float32ne,
221     [PA_SAMPLE_S16LE]     = (pa_convert_func_t) pa_sconv_s16le_from_float32ne,
222     [PA_SAMPLE_S16BE]     = (pa_convert_func_t) pa_sconv_s16be_from_float32ne,
223     [PA_SAMPLE_S32LE]     = (pa_convert_func_t) pa_sconv_s32le_from_float32ne,
224     [PA_SAMPLE_S32BE]     = (pa_convert_func_t) pa_sconv_s32be_from_float32ne,
225     [PA_SAMPLE_S24LE]     = (pa_convert_func_t) pa_sconv_s24le_from_float32ne,
226     [PA_SAMPLE_S24BE]     = (pa_convert_func_t) pa_sconv_s24be_from_float32ne,
227     [PA_SAMPLE_S24_32LE]  = (pa_convert_func_t) pa_sconv_s24_32le_from_float32ne,
228     [PA_SAMPLE_S24_32BE]  = (pa_convert_func_t) pa_sconv_s24_32be_from_float32ne,
229     [PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
230     [PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
231     [PA_SAMPLE_ALAW]      = (pa_convert_func_t) alaw_from_float32ne,
232     [PA_SAMPLE_ULAW]      = (pa_convert_func_t) ulaw_from_float32ne
233 };
234
235 pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
236
237     pa_assert(f >= 0);
238     pa_assert(f < PA_SAMPLE_MAX);
239
240     return from_float32ne_table[f];
241 }
242
243 void pa_set_convert_from_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
244
245     pa_assert(f >= 0);
246     pa_assert(f < PA_SAMPLE_MAX);
247
248     from_float32ne_table[f] = func;
249 }
250
251 static pa_convert_func_t to_s16ne_table[] = {
252     [PA_SAMPLE_U8]        = (pa_convert_func_t) u8_to_s16ne,
253     [PA_SAMPLE_S16NE]     = (pa_convert_func_t) s16ne_to_s16ne,
254     [PA_SAMPLE_S16RE]     = (pa_convert_func_t) s16re_to_s16ne,
255     [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_to_s16ne,
256     [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_to_s16ne,
257     [PA_SAMPLE_S32BE]     = (pa_convert_func_t) pa_sconv_s32be_to_s16ne,
258     [PA_SAMPLE_S32LE]     = (pa_convert_func_t) pa_sconv_s32le_to_s16ne,
259     [PA_SAMPLE_S24BE]     = (pa_convert_func_t) pa_sconv_s24be_to_s16ne,
260     [PA_SAMPLE_S24LE]     = (pa_convert_func_t) pa_sconv_s24le_to_s16ne,
261     [PA_SAMPLE_S24_32BE]  = (pa_convert_func_t) pa_sconv_s24_32be_to_s16ne,
262     [PA_SAMPLE_S24_32LE]  = (pa_convert_func_t) pa_sconv_s24_32le_to_s16ne,
263     [PA_SAMPLE_ALAW]      = (pa_convert_func_t) alaw_to_s16ne,
264     [PA_SAMPLE_ULAW]      = (pa_convert_func_t) ulaw_to_s16ne
265 };
266
267 pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
268
269     pa_assert(f >= 0);
270     pa_assert(f < PA_SAMPLE_MAX);
271
272     return to_s16ne_table[f];
273 }
274
275 void pa_set_convert_to_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
276
277     pa_assert(f >= 0);
278     pa_assert(f < PA_SAMPLE_MAX);
279
280     to_s16ne_table[f] = func;
281 }
282
283 static pa_convert_func_t from_s16ne_table[] = {
284     [PA_SAMPLE_U8]        = (pa_convert_func_t) u8_from_s16ne,
285     [PA_SAMPLE_S16NE]     = (pa_convert_func_t) s16ne_to_s16ne,
286     [PA_SAMPLE_S16RE]     = (pa_convert_func_t) s16re_to_s16ne,
287     [PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_from_s16ne,
288     [PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_from_s16ne,
289     [PA_SAMPLE_S32BE]     = (pa_convert_func_t) pa_sconv_s32be_from_s16ne,
290     [PA_SAMPLE_S32LE]     = (pa_convert_func_t) pa_sconv_s32le_from_s16ne,
291     [PA_SAMPLE_S24BE]     = (pa_convert_func_t) pa_sconv_s24be_from_s16ne,
292     [PA_SAMPLE_S24LE]     = (pa_convert_func_t) pa_sconv_s24le_from_s16ne,
293     [PA_SAMPLE_S24_32BE]  = (pa_convert_func_t) pa_sconv_s24_32be_from_s16ne,
294     [PA_SAMPLE_S24_32LE]  = (pa_convert_func_t) pa_sconv_s24_32le_from_s16ne,
295     [PA_SAMPLE_ALAW]      = (pa_convert_func_t) alaw_from_s16ne,
296     [PA_SAMPLE_ULAW]      = (pa_convert_func_t) ulaw_from_s16ne,
297 };
298
299 pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
300
301     pa_assert(f >= 0);
302     pa_assert(f < PA_SAMPLE_MAX);
303
304     return from_s16ne_table[f];
305 }
306
307 void pa_set_convert_from_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
308
309     pa_assert(f >= 0);
310     pa_assert(f < PA_SAMPLE_MAX);
311
312     from_s16ne_table[f] = func;
313 }