xref: /netbsd/usr.bin/audio/common/auconv.h (revision c4a72b64)
1 /*	$NetBSD: auconv.h,v 1.2 2002/10/13 00:59:45 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/audioio.h>
41 
42 /* Convert between signed and unsigned. */
43 static __inline__ void change_sign8(u_char *, size_t);
44 static __inline__ void change_sign16_le(u_char *, size_t);
45 static __inline__ void change_sign16_be(u_char *, size_t);
46 static __inline__ void change_sign32_le(u_char *, size_t);
47 static __inline__ void change_sign32_be(u_char *, size_t);
48 /* Convert between little and big endian. */
49 static __inline__ void swap_bytes(u_char *, size_t);
50 static __inline__ void swap_bytes32(u_char *, size_t);
51 static __inline__ void swap_bytes_change_sign16_le(u_char *, size_t);
52 static __inline__ void swap_bytes_change_sign16_be(u_char *, size_t);
53 static __inline__ void change_sign16_swap_bytes_le(u_char *, size_t);
54 static __inline__ void change_sign16_swap_bytes_be(u_char *, size_t);
55 static __inline__ void swap_bytes_change_sign32_le(u_char *, size_t);
56 static __inline__ void swap_bytes_change_sign32_be(u_char *, size_t);
57 static __inline__ void change_sign32_swap_bytes_le(u_char *, size_t);
58 static __inline__ void change_sign32_swap_bytes_be(u_char *, size_t);
59 
60 static __inline__ void
61 change_sign8(u_char *p, size_t cc)
62 {
63 	while (--cc > 0) {
64 		*p ^= 0x80;
65 		++p;
66 	}
67 }
68 
69 static __inline__ void
70 change_sign16_le(u_char *p, size_t cc)
71 {
72 	while ((cc -= 2) > 0) {
73 		p[1] ^= 0x80;
74 		p += 2;
75 	}
76 }
77 
78 static __inline__ void
79 change_sign16_be(u_char *p, size_t cc)
80 {
81 	while ((cc -= 2) > 0) {
82 		p[0] ^= 0x80;
83 		p += 2;
84 	}
85 }
86 
87 static __inline__ void
88 change_sign32_le(u_char *p, size_t cc)
89 {
90 	while ((cc -= 4) > 0) {
91 		p[3] ^= 0x80;
92 		p += 4;
93 	}
94 }
95 
96 static __inline__ void
97 change_sign32_be(u_char *p, size_t cc)
98 {
99 	while ((cc -= 4) > 0) {
100 		p[0] ^= 0x80;
101 		p += 4;
102 	}
103 }
104 
105 static __inline__ void
106 swap_bytes(u_char *p, size_t cc)
107 {
108 	u_char t;
109 
110 	while ((cc -= 2) > 0) {
111 		t = p[0];
112 		p[0] = p[1];
113 		p[1] = t;
114 		p += 2;
115 	}
116 }
117 
118 static __inline__ void
119 swap_bytes32(u_char *p, size_t cc)
120 {
121 	u_char t;
122 
123 	while ((cc -= 4) > 0) {
124 		t = p[0];
125 		p[0] = p[3];
126 		p[3] = t;
127 		t = p[1];
128 		p[1] = p[2];
129 		p[2] = t;
130 		p += 4;
131 	}
132 }
133 
134 static __inline__ void
135 swap_bytes_change_sign16_le(u_char *p, size_t cc)
136 {
137 	u_char t;
138 
139 	while ((cc -= 2) > 0) {
140 		t = p[1];
141 		p[1] = p[0] ^ 0x80;
142 		p[0] = t;
143 		p += 2;
144 	}
145 }
146 
147 static __inline__ void
148 swap_bytes_change_sign16_be(u_char *p, size_t cc)
149 {
150 	u_char t;
151 
152 	while ((cc -= 2) > 0) {
153 		t = p[0];
154 		p[0] = p[1] ^ 0x80;
155 		p[1] = t;
156 		p += 2;
157 	}
158 }
159 
160 static __inline__ void
161 change_sign16_swap_bytes_le(u_char *p, size_t cc)
162 {
163 	swap_bytes_change_sign16_be(p, cc);
164 }
165 
166 static __inline__ void
167 change_sign16_swap_bytes_be(u_char *p, size_t cc)
168 {
169 	swap_bytes_change_sign16_le(p, cc);
170 }
171 
172 static __inline__ void
173 swap_bytes_change_sign32_le(u_char *p, size_t cc)
174 {
175 	u_char t;
176 
177 	while ((cc -= 4) > 0) {
178 		t = p[3];
179 		p[3] = p[0] ^ 0x80;
180 		p[0] = t;
181 		t = p[1];
182 		p[1] = p[2];
183 		p[2] = t;
184 		p += 4;
185 	}
186 }
187 
188 static __inline__ void
189 swap_bytes_change_sign32_be(u_char *p, size_t cc)
190 {
191 	u_char t;
192 
193 	while ((cc -= 4) > 0) {
194 		t = p[0];
195 		p[0] = p[3] ^ 0x80;
196 		p[3] = t;
197 		t = p[1];
198 		p[1] = p[2];
199 		p[2] = t;
200 		p += 4;
201 	}
202 }
203 
204 static __inline__ void
205 change_sign32_swap_bytes_le(u_char *p, size_t cc)
206 {
207 	swap_bytes_change_sign32_be(p, cc);
208 }
209 
210 static __inline__ void
211 change_sign32_swap_bytes_be(u_char *p, size_t cc)
212 {
213 	swap_bytes_change_sign32_le(p, cc);
214 }
215