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