]> code.delx.au - pulseaudio/blob - src/pulsecore/sconv.c
Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[pulseaudio] / src / pulsecore / sconv.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <liboil/liboilfuncs.h>
31 #include <liboil/liboil.h>
32
33 #include <pulsecore/g711.h>
34
35 #include "endianmacros.h"
36 #include "sconv-s16le.h"
37 #include "sconv-s16be.h"
38
39 #include "sconv.h"
40
41 static void u8_to_float32ne(unsigned n, const void *a, float *b) {
42 const uint8_t *ca = a;
43 static const double add = -128.0/127.0, factor = 1.0/127.0;
44
45 assert(a);
46 assert(b);
47
48 oil_scaleconv_f32_u8(b, ca, n, &add, &factor);
49 }
50
51 static void u8_from_float32ne(unsigned n, const float *a, void *b) {
52 uint8_t *cb = b;
53 static const double add = 128.0, factor = 127.0;
54
55 assert(a);
56 assert(b);
57
58 oil_scaleconv_u8_f32(cb, a, n, &add, &factor);
59 }
60
61 static void float32ne_to_float32ne(unsigned n, const void *a, float *b) {
62 assert(a);
63 assert(b);
64
65 oil_memcpy(b, a, sizeof(float) * n);
66 }
67
68 static void float32ne_from_float32ne(unsigned n, const float *a, void *b) {
69 assert(a);
70 assert(b);
71
72 oil_memcpy(b, a, sizeof(float) * n);
73 }
74
75 static void float32re_to_float32ne(unsigned n, const void *a, float *b) {
76 assert(a);
77 assert(b);
78
79 while (n-- > 0)
80 ((uint32_t *)b)[n] = UINT32_SWAP (((uint32_t *)a)[n]);
81 }
82
83 static void float32re_from_float32ne(unsigned n, const float *a, void *b) {
84 assert(a);
85 assert(b);
86
87 while (n-- > 0)
88 ((uint32_t *)b)[n] = UINT32_SWAP (((uint32_t *)a)[n]);
89 }
90
91 static void ulaw_to_float32ne(unsigned n, const void *a, float *b) {
92 const uint8_t *ca = a;
93
94 assert(a);
95 assert(b);
96
97 for (; n > 0; n--)
98 *(b++) = st_ulaw2linear16(*(ca++)) * 1.0F / 0x7FFF;
99 }
100
101 static void ulaw_from_float32ne(unsigned n, const float *a, void *b) {
102 uint8_t *cb = b;
103
104 assert(a);
105 assert(b);
106
107 for (; n > 0; n--) {
108 float v = *(a++);
109
110 if (v > 1)
111 v = 1;
112
113 if (v < -1)
114 v = -1;
115
116 *(cb++) = st_14linear2ulaw((int16_t) (v * 0x1FFF));
117 }
118 }
119
120 static void alaw_to_float32ne(unsigned n, const void *a, float *b) {
121 const uint8_t *ca = a;
122
123 assert(a);
124 assert(b);
125
126 for (; n > 0; n--)
127 *(b++) = st_alaw2linear16(*(ca++)) * 1.0F / 0x7FFF;
128 }
129
130 static void alaw_from_float32ne(unsigned n, const float *a, void *b) {
131 uint8_t *cb = b;
132
133 assert(a);
134 assert(b);
135
136 for (; n > 0; n--) {
137 float v = *(a++);
138
139 if (v > 1)
140 v = 1;
141
142 if (v < -1)
143 v = -1;
144
145 *(cb++) = st_13linear2alaw((int16_t) (v * 0xFFF));
146 }
147 }
148
149 pa_convert_to_float32ne_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
150 switch(f) {
151 case PA_SAMPLE_U8:
152 return u8_to_float32ne;
153 case PA_SAMPLE_S16LE:
154 return pa_sconv_s16le_to_float32ne;
155 case PA_SAMPLE_S16BE:
156 return pa_sconv_s16be_to_float32ne;
157 case PA_SAMPLE_FLOAT32NE:
158 return float32ne_to_float32ne;
159 case PA_SAMPLE_FLOAT32RE:
160 return float32re_to_float32ne;
161 case PA_SAMPLE_ALAW:
162 return alaw_to_float32ne;
163 case PA_SAMPLE_ULAW:
164 return ulaw_to_float32ne;
165 default:
166 return NULL;
167 }
168 }
169
170 pa_convert_from_float32ne_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
171 switch(f) {
172 case PA_SAMPLE_U8:
173 return u8_from_float32ne;
174 case PA_SAMPLE_S16LE:
175 return pa_sconv_s16le_from_float32ne;
176 case PA_SAMPLE_S16BE:
177 return pa_sconv_s16be_from_float32ne;
178 case PA_SAMPLE_FLOAT32NE:
179 return float32ne_from_float32ne;
180 case PA_SAMPLE_FLOAT32RE:
181 return float32re_from_float32ne;
182 case PA_SAMPLE_ALAW:
183 return alaw_from_float32ne;
184 case PA_SAMPLE_ULAW:
185 return ulaw_from_float32ne;
186 default:
187 return NULL;
188 }
189 }