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