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