FreeBSD kernel sound device code
g711.h
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31#ifndef _SND_G711_H_
32#define _SND_G711_H_
33
34#define G711_TABLE_SIZE 256
35
36#define ULAW_TO_U8 { \
37 3, 7, 11, 15, 19, 23, 27, 31, \
38 35, 39, 43, 47, 51, 55, 59, 63, \
39 66, 68, 70, 72, 74, 76, 78, 80, \
40 82, 84, 86, 88, 90, 92, 94, 96, \
41 98, 99, 100, 101, 102, 103, 104, 105, \
42 106, 107, 108, 109, 110, 111, 112, 113, \
43 113, 114, 114, 115, 115, 116, 116, 117, \
44 117, 118, 118, 119, 119, 120, 120, 121, \
45 121, 121, 122, 122, 122, 122, 123, 123, \
46 123, 123, 124, 124, 124, 124, 125, 125, \
47 125, 125, 125, 125, 126, 126, 126, 126, \
48 126, 126, 126, 126, 127, 127, 127, 127, \
49 127, 127, 127, 127, 127, 127, 127, 127, \
50 128, 128, 128, 128, 128, 128, 128, 128, \
51 128, 128, 128, 128, 128, 128, 128, 128, \
52 128, 128, 128, 128, 128, 128, 128, 128, \
53 253, 249, 245, 241, 237, 233, 229, 225, \
54 221, 217, 213, 209, 205, 201, 197, 193, \
55 190, 188, 186, 184, 182, 180, 178, 176, \
56 174, 172, 170, 168, 166, 164, 162, 160, \
57 158, 157, 156, 155, 154, 153, 152, 151, \
58 150, 149, 148, 147, 146, 145, 144, 143, \
59 143, 142, 142, 141, 141, 140, 140, 139, \
60 139, 138, 138, 137, 137, 136, 136, 135, \
61 135, 135, 134, 134, 134, 134, 133, 133, \
62 133, 133, 132, 132, 132, 132, 131, 131, \
63 131, 131, 131, 131, 130, 130, 130, 130, \
64 130, 130, 130, 130, 129, 129, 129, 129, \
65 129, 129, 129, 129, 129, 129, 129, 129, \
66 128, 128, 128, 128, 128, 128, 128, 128, \
67 128, 128, 128, 128, 128, 128, 128, 128, \
68 128, 128, 128, 128, 128, 128, 128, 128, \
69 }
70
71#define ALAW_TO_U8 { \
72 108, 109, 106, 107, 112, 113, 110, 111, \
73 100, 101, 98, 99, 104, 105, 102, 103, \
74 118, 118, 117, 117, 120, 120, 119, 119, \
75 114, 114, 113, 113, 116, 116, 115, 115, \
76 43, 47, 35, 39, 59, 63, 51, 55, \
77 11, 15, 3, 7, 27, 31, 19, 23, \
78 86, 88, 82, 84, 94, 96, 90, 92, \
79 70, 72, 66, 68, 78, 80, 74, 76, \
80 127, 127, 127, 127, 127, 127, 127, 127, \
81 127, 127, 127, 127, 127, 127, 127, 127, \
82 128, 128, 128, 128, 128, 128, 128, 128, \
83 128, 128, 128, 128, 128, 128, 128, 128, \
84 123, 123, 123, 123, 124, 124, 124, 124, \
85 121, 121, 121, 121, 122, 122, 122, 122, \
86 126, 126, 126, 126, 126, 126, 126, 126, \
87 125, 125, 125, 125, 125, 125, 125, 125, \
88 148, 147, 150, 149, 144, 143, 146, 145, \
89 156, 155, 158, 157, 152, 151, 154, 153, \
90 138, 138, 139, 139, 136, 136, 137, 137, \
91 142, 142, 143, 143, 140, 140, 141, 141, \
92 213, 209, 221, 217, 197, 193, 205, 201, \
93 245, 241, 253, 249, 229, 225, 237, 233, \
94 170, 168, 174, 172, 162, 160, 166, 164, \
95 186, 184, 190, 188, 178, 176, 182, 180, \
96 129, 129, 129, 129, 129, 129, 129, 129, \
97 129, 129, 129, 129, 129, 129, 129, 129, \
98 128, 128, 128, 128, 128, 128, 128, 128, \
99 128, 128, 128, 128, 128, 128, 128, 128, \
100 133, 133, 133, 133, 132, 132, 132, 132, \
101 135, 135, 135, 135, 134, 134, 134, 134, \
102 130, 130, 130, 130, 130, 130, 130, 130, \
103 131, 131, 131, 131, 131, 131, 131, 131, \
104 }
105
106#define U8_TO_ULAW { \
107 0, 0, 0, 0, 0, 1, 1, 1, \
108 1, 2, 2, 2, 2, 3, 3, 3, \
109 3, 4, 4, 4, 4, 5, 5, 5, \
110 5, 6, 6, 6, 6, 7, 7, 7, \
111 7, 8, 8, 8, 8, 9, 9, 9, \
112 9, 10, 10, 10, 10, 11, 11, 11, \
113 11, 12, 12, 12, 12, 13, 13, 13, \
114 13, 14, 14, 14, 14, 15, 15, 15, \
115 15, 16, 16, 17, 17, 18, 18, 19, \
116 19, 20, 20, 21, 21, 22, 22, 23, \
117 23, 24, 24, 25, 25, 26, 26, 27, \
118 27, 28, 28, 29, 29, 30, 30, 31, \
119 31, 32, 33, 34, 35, 36, 37, 38, \
120 39, 40, 41, 42, 43, 44, 45, 46, \
121 47, 49, 51, 53, 55, 57, 59, 61, \
122 63, 66, 70, 74, 78, 84, 92, 104, \
123 254, 231, 219, 211, 205, 201, 197, 193, \
124 190, 188, 186, 184, 182, 180, 178, 176, \
125 175, 174, 173, 172, 171, 170, 169, 168, \
126 167, 166, 165, 164, 163, 162, 161, 160, \
127 159, 159, 158, 158, 157, 157, 156, 156, \
128 155, 155, 154, 154, 153, 153, 152, 152, \
129 151, 151, 150, 150, 149, 149, 148, 148, \
130 147, 147, 146, 146, 145, 145, 144, 144, \
131 143, 143, 143, 143, 142, 142, 142, 142, \
132 141, 141, 141, 141, 140, 140, 140, 140, \
133 139, 139, 139, 139, 138, 138, 138, 138, \
134 137, 137, 137, 137, 136, 136, 136, 136, \
135 135, 135, 135, 135, 134, 134, 134, 134, \
136 133, 133, 133, 133, 132, 132, 132, 132, \
137 131, 131, 131, 131, 130, 130, 130, 130, \
138 129, 129, 129, 129, 128, 128, 128, 128, \
139 }
140
141#define U8_TO_ALAW { \
142 42, 42, 42, 42, 42, 43, 43, 43, \
143 43, 40, 40, 40, 40, 41, 41, 41, \
144 41, 46, 46, 46, 46, 47, 47, 47, \
145 47, 44, 44, 44, 44, 45, 45, 45, \
146 45, 34, 34, 34, 34, 35, 35, 35, \
147 35, 32, 32, 32, 32, 33, 33, 33, \
148 33, 38, 38, 38, 38, 39, 39, 39, \
149 39, 36, 36, 36, 36, 37, 37, 37, \
150 37, 58, 58, 59, 59, 56, 56, 57, \
151 57, 62, 62, 63, 63, 60, 60, 61, \
152 61, 50, 50, 51, 51, 48, 48, 49, \
153 49, 54, 54, 55, 55, 52, 52, 53, \
154 53, 10, 11, 8, 9, 14, 15, 12, \
155 13, 2, 3, 0, 1, 6, 7, 4, \
156 5, 24, 30, 28, 18, 16, 22, 20, \
157 106, 110, 98, 102, 122, 114, 75, 90, \
158 213, 197, 245, 253, 229, 225, 237, 233, \
159 149, 151, 145, 147, 157, 159, 153, 155, \
160 133, 132, 135, 134, 129, 128, 131, 130, \
161 141, 140, 143, 142, 137, 136, 139, 138, \
162 181, 181, 180, 180, 183, 183, 182, 182, \
163 177, 177, 176, 176, 179, 179, 178, 178, \
164 189, 189, 188, 188, 191, 191, 190, 190, \
165 185, 185, 184, 184, 187, 187, 186, 186, \
166 165, 165, 165, 165, 164, 164, 164, 164, \
167 167, 167, 167, 167, 166, 166, 166, 166, \
168 161, 161, 161, 161, 160, 160, 160, 160, \
169 163, 163, 163, 163, 162, 162, 162, 162, \
170 173, 173, 173, 173, 172, 172, 172, 172, \
171 175, 175, 175, 175, 174, 174, 174, 174, \
172 169, 169, 169, 169, 168, 168, 168, 168, \
173 171, 171, 171, 171, 170, 170, 170, 170, \
174 }
175
176#define _G711_TO_INTPCM(t, v) ((intpcm_t) \
177 ((int8_t)((t)[(uint8_t)(v)] ^ 0x80)))
178
179#define _INTPCM_TO_G711(t, v) ((t)[(uint8_t)((v) ^ 0x80)])
180
181#define G711_DECLARE_TABLE(t) \
182static const struct { \
183 const uint8_t ulaw_to_u8[G711_TABLE_SIZE]; \
184 const uint8_t alaw_to_u8[G711_TABLE_SIZE]; \
185 const uint8_t u8_to_ulaw[G711_TABLE_SIZE]; \
186 const uint8_t u8_to_alaw[G711_TABLE_SIZE]; \
187} t = { \
188 ULAW_TO_U8, ALAW_TO_U8, \
189 U8_TO_ULAW, U8_TO_ALAW \
190}
191
192#define G711_DECLARE_OP(t) \
193static __inline intpcm_t \
194pcm_read_ulaw(uint8_t v) \
195{ \
196 \
197 return (_G711_TO_INTPCM((t).ulaw_to_u8, v)); \
198} \
199 \
200static __inline intpcm_t \
201pcm_read_alaw(uint8_t v) \
202{ \
203 \
204 return (_G711_TO_INTPCM((t).alaw_to_u8, v)); \
205} \
206 \
207static __inline void \
208pcm_write_ulaw(uint8_t *dst, intpcm_t v) \
209{ \
210 \
211 *dst = _INTPCM_TO_G711((t).u8_to_ulaw, v); \
212} \
213 \
214static __inline void \
215pcm_write_alaw(uint8_t *dst, intpcm_t v) \
216{ \
217 \
218 *dst = _INTPCM_TO_G711((t).u8_to_alaw, v); \
219}
220
221#define G711_DECLARE(t) \
222 G711_DECLARE_TABLE(t); \
223 G711_DECLARE_OP(t)
224
225#endif /* !_SND_G711_H_ */