1 /*	$OpenBSD: auconv.c,v 1.6 2003/06/27 00:23:43 jason Exp $ */
2 /*	$NetBSD: auconv.c,v 1.3 1999/11/01 18:12:19 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the Computer Systems
19  *	Engineering Group at Lawrence Berkeley Laboratory.
20  * 4. Neither the name of the University nor of the Laboratory may be used
21  *    to endorse or promote products derived from this software without
22  *    specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #include <sys/types.h>
39 #include <sys/audioio.h>
40 #include <dev/auconv.h>
41 
42 void
change_sign8(void * v,u_char * p,int cc)43 change_sign8(void *v, u_char *p, int cc)
44 {
45 	while (--cc >= 0) {
46 		*p ^= 0x80;
47 		++p;
48 	}
49 }
50 
51 void
change_sign16_le(void * v,u_char * p,int cc)52 change_sign16_le(void *v, u_char *p, int cc)
53 {
54 	while ((cc -= 2) >= 0) {
55 		p[1] ^= 0x80;
56 		p += 2;
57 	}
58 }
59 
60 void
change_sign16_be(void * v,u_char * p,int cc)61 change_sign16_be(void *v, u_char *p, int cc)
62 {
63 	while ((cc -= 2) >= 0) {
64 		p[0] ^= 0x80;
65 		p += 2;
66 	}
67 }
68 
69 void
swap_bytes(void * v,u_char * p,int cc)70 swap_bytes(void *v, u_char *p, int cc)
71 {
72 	u_char t;
73 
74 	while ((cc -= 2) >= 0) {
75 		t = p[0];
76 		p[0] = p[1];
77 		p[1] = t;
78 		p += 2;
79 	}
80 }
81 
82 void
swap_bytes_change_sign16_le(void * v,u_char * p,int cc)83 swap_bytes_change_sign16_le(void *v, u_char *p, int cc)
84 {
85 	u_char t;
86 
87 	while ((cc -= 2) >= 0) {
88 		t = p[1];
89 		p[1] = p[0] ^ 0x80;
90 		p[0] = t;
91 		p += 2;
92 	}
93 }
94 
95 void
swap_bytes_change_sign16_be(void * v,u_char * p,int cc)96 swap_bytes_change_sign16_be(void *v, u_char *p, int cc)
97 {
98 	u_char t;
99 
100 	while ((cc -= 2) >= 0) {
101 		t = p[0];
102 		p[0] = p[1] ^ 0x80;
103 		p[1] = t;
104 		p += 2;
105 	}
106 }
107 
108 void
change_sign16_swap_bytes_le(void * v,u_char * p,int cc)109 change_sign16_swap_bytes_le(void *v, u_char *p, int cc)
110 {
111 	swap_bytes_change_sign16_be(v, p, cc);
112 }
113 
114 void
change_sign16_swap_bytes_be(void * v,u_char * p,int cc)115 change_sign16_swap_bytes_be(void *v, u_char *p, int cc)
116 {
117 	swap_bytes_change_sign16_le(v, p, cc);
118 }
119 
120 void
linear8_to_linear16_le(void * v,u_char * p,int cc)121 linear8_to_linear16_le(void *v, u_char *p, int cc)
122 {
123 	u_char *q = p;
124 
125 	p += cc;
126 	q += cc * 2;
127 	while (--cc >= 0) {
128 		q -= 2;
129 		q[1] = *--p;
130 		q[0] = 0;
131 	}
132 }
133 
134 void
linear8_to_linear16_be(void * v,u_char * p,int cc)135 linear8_to_linear16_be(void *v, u_char *p, int cc)
136 {
137 	u_char *q = p;
138 
139 	p += cc;
140 	q += cc * 2;
141 	while (--cc >= 0) {
142 		q -= 2;
143 		q[0] = *--p;
144 		q[1] = 0;
145 	}
146 }
147 
148 void
linear16_to_linear8_le(void * v,u_char * p,int cc)149 linear16_to_linear8_le(void *v, u_char *p, int cc)
150 {
151 	u_char *q = p;
152 
153 	while ((cc -= 2) >= 0) {
154 		*q++ = p[1];
155 		p += 2;
156 	}
157 }
158 
159 void
linear16_to_linear8_be(void * v,u_char * p,int cc)160 linear16_to_linear8_be(void *v, u_char *p, int cc)
161 {
162 	u_char *q = p;
163 
164 	while ((cc -= 2) >= 0) {
165 		*q++ = p[0];
166 		p += 2;
167 	}
168 }
169 
170 void
ulinear8_to_linear16_le(void * v,u_char * p,int cc)171 ulinear8_to_linear16_le(void *v, u_char *p, int cc)
172 {
173 	u_char *q = p;
174 
175 	p += cc;
176 	q += cc * 2;
177 	while (--cc >= 0) {
178 		q -= 2;
179 		q[1] = (*--p) ^ 0x80;
180 		q[0] = 0;
181 	}
182 }
183 
184 void
ulinear8_to_linear16_be(void * v,u_char * p,int cc)185 ulinear8_to_linear16_be(void *v, u_char *p, int cc)
186 {
187 	u_char *q = p;
188 
189 	p += cc;
190 	q += cc * 2;
191 	while (--cc >= 0) {
192 		q -= 2;
193 		q[0] = (*--p) ^ 0x80;
194 		q[1] = 0;
195 	}
196 }
197 
198 void
linear16_to_ulinear8_le(void * v,u_char * p,int cc)199 linear16_to_ulinear8_le(void *v, u_char *p, int cc)
200 {
201 	u_char *q = p;
202 
203 	while (--cc >= 0) {
204 		*q++ = p[1] ^ 0x80;
205 		p += 2;
206 	}
207 }
208 
209 void
linear16_to_ulinear8_be(void * v,u_char * p,int cc)210 linear16_to_ulinear8_be(void *v, u_char *p, int cc)
211 {
212 	u_char *q = p;
213 
214 	while (--cc >= 0) {
215 		*q++ = p[0] ^ 0x80;
216 		p += 2;
217 	}
218 }
219 
220 /*
221  * just expand mono to stereo, no other conversions
222  */
223 void
noswap_bytes_mts(void * v,u_char * p,int cc)224 noswap_bytes_mts(void *v, u_char *p, int cc)
225 {
226 	u_char *q = p;
227 
228 	p += cc;
229 	q += cc * 2;
230 	while ((cc -= 2) >= 0) {
231 		q -= 4;
232 		q[1] = q[3] = *--p;
233 		q[0] = q[2] = *--p;
234 	}
235 }
236 
237 /*
238  * same as swap_bytes(), just expand mono to stereo
239  */
240 void
swap_bytes_mts(void * v,u_char * p,int cc)241 swap_bytes_mts(void *v, u_char *p, int cc)
242 {
243 	u_char *q = p;
244 
245 	p += cc;
246 	q += cc * 2;
247 	while ((cc -= 2) >= 0) {
248 		q -= 4;
249 		q[0] = q[2] = *--p;
250 		q[1] = q[3] = *--p;
251 	}
252 }
253 
254 /*
255  * same as linear8_to_linear16_le(), plus expand mono to stereo
256  */
257 void
linear8_to_linear16_le_mts(void * v,u_char * p,int cc)258 linear8_to_linear16_le_mts(void *v, u_char *p, int cc)
259 {
260 	u_char *q = p;
261 
262 	p += cc;
263 	q += cc * 4;
264 	while (--cc >= 0) {
265 		q -= 4;
266 		q[1] = q[3] = *--p;
267 		q[0] = q[2] = 0;
268 	}
269 }
270 
271 /*
272  * same as linear8_to_linear16_be(), plus expand mono to stereo
273  */
274 void
linear8_to_linear16_be_mts(void * v,u_char * p,int cc)275 linear8_to_linear16_be_mts(void *v, u_char *p, int cc)
276 {
277 	u_char *q = p;
278 
279 	p += cc;
280 	q += cc * 4;
281 	while (--cc >= 0) {
282 		q -= 4;
283 		q[0] = q[2] = *--p;
284 		q[1] = q[3] = 0;
285 	}
286 }
287 
288 /*
289  * same as ulinear8_to_linear16_le(), plus expand mono to stereo
290  */
291 void
ulinear8_to_linear16_le_mts(void * v,u_char * p,int cc)292 ulinear8_to_linear16_le_mts(void *v, u_char *p, int cc)
293 {
294 	u_char *q = p;
295 
296 	p += cc;
297 	q += cc * 4;
298 	while (--cc >= 0) {
299 		q -= 4;
300 		q[1] = q[3] = (*--p) ^ 0x80;
301 		q[0] = q[2] = 0;
302 	}
303 }
304 
305 /*
306  * same as ulinear8_to_linear16_be(), plus expand mono to stereo
307  */
308 void
ulinear8_to_linear16_be_mts(void * v,u_char * p,int cc)309 ulinear8_to_linear16_be_mts(void *v, u_char *p, int cc)
310 {
311 	u_char *q = p;
312 
313 	p += cc;
314 	q += cc * 4;
315 	while (--cc >= 0) {
316 		q -= 4;
317 		q[0] = q[2] = (*--p) ^ 0x80;
318 		q[1] = q[3] = 0;
319 	}
320 }
321 
322 /*
323  * same as change_sign16_le(), plus expand mono to stereo
324  */
325 void
change_sign16_le_mts(void * v,u_char * p,int cc)326 change_sign16_le_mts(void *v, u_char *p, int cc)
327 {
328 	u_char *q = p;
329 
330 	p += cc;
331 	q += cc * 2;
332 	while ((cc -= 2) >= 0) {
333 		q -= 4;
334 		q[1] = q[3] = (*--p) ^ 0x80;
335 		q[0] = q[2] = *--p;
336 	}
337 }
338 
339 /*
340  * same as change_sign16_be(), plus expand mono to stereo
341  */
342 void
change_sign16_be_mts(void * v,u_char * p,int cc)343 change_sign16_be_mts(void *v, u_char *p, int cc)
344 {
345 	u_char *q = p;
346 
347 	p += cc;
348 	q += cc * 2;
349 	while ((cc -= 2) >= 0) {
350 		q -= 4;
351 		q[0] = q[2] = (*--p) ^ 0x80;
352 		q[1] = q[3] = *--p;
353 	}
354 }
355 
356 /*
357  *  same as change_sign16_swap_bytes_le(), plus expand mono to stereo
358  */
359 void
change_sign16_swap_bytes_le_mts(void * v,u_char * p,int cc)360 change_sign16_swap_bytes_le_mts(void *v, u_char *p, int cc)
361 {
362 	change_sign16_be_mts(v, p, cc);
363 }
364 
365 /*
366  * same as change_sign16_swap_bytes_be(), plus expand mono to stereo
367  */
368 void
change_sign16_swap_bytes_be_mts(void * v,u_char * p,int cc)369 change_sign16_swap_bytes_be_mts(void *v, u_char *p, int cc)
370 {
371 	change_sign16_le_mts(v, p, cc);
372 }
373