1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 29 #ifndef _SND_G711_H_ 30 #define _SND_G711_H_ 31 32 #define G711_TABLE_SIZE 256 33 34 #define ULAW_TO_U8 { \ 35 3, 7, 11, 15, 19, 23, 27, 31, \ 36 35, 39, 43, 47, 51, 55, 59, 63, \ 37 66, 68, 70, 72, 74, 76, 78, 80, \ 38 82, 84, 86, 88, 90, 92, 94, 96, \ 39 98, 99, 100, 101, 102, 103, 104, 105, \ 40 106, 107, 108, 109, 110, 111, 112, 113, \ 41 113, 114, 114, 115, 115, 116, 116, 117, \ 42 117, 118, 118, 119, 119, 120, 120, 121, \ 43 121, 121, 122, 122, 122, 122, 123, 123, \ 44 123, 123, 124, 124, 124, 124, 125, 125, \ 45 125, 125, 125, 125, 126, 126, 126, 126, \ 46 126, 126, 126, 126, 127, 127, 127, 127, \ 47 127, 127, 127, 127, 127, 127, 127, 127, \ 48 128, 128, 128, 128, 128, 128, 128, 128, \ 49 128, 128, 128, 128, 128, 128, 128, 128, \ 50 128, 128, 128, 128, 128, 128, 128, 128, \ 51 253, 249, 245, 241, 237, 233, 229, 225, \ 52 221, 217, 213, 209, 205, 201, 197, 193, \ 53 190, 188, 186, 184, 182, 180, 178, 176, \ 54 174, 172, 170, 168, 166, 164, 162, 160, \ 55 158, 157, 156, 155, 154, 153, 152, 151, \ 56 150, 149, 148, 147, 146, 145, 144, 143, \ 57 143, 142, 142, 141, 141, 140, 140, 139, \ 58 139, 138, 138, 137, 137, 136, 136, 135, \ 59 135, 135, 134, 134, 134, 134, 133, 133, \ 60 133, 133, 132, 132, 132, 132, 131, 131, \ 61 131, 131, 131, 131, 130, 130, 130, 130, \ 62 130, 130, 130, 130, 129, 129, 129, 129, \ 63 129, 129, 129, 129, 129, 129, 129, 129, \ 64 128, 128, 128, 128, 128, 128, 128, 128, \ 65 128, 128, 128, 128, 128, 128, 128, 128, \ 66 128, 128, 128, 128, 128, 128, 128, 128, \ 67 } 68 69 #define ALAW_TO_U8 { \ 70 108, 109, 106, 107, 112, 113, 110, 111, \ 71 100, 101, 98, 99, 104, 105, 102, 103, \ 72 118, 118, 117, 117, 120, 120, 119, 119, \ 73 114, 114, 113, 113, 116, 116, 115, 115, \ 74 43, 47, 35, 39, 59, 63, 51, 55, \ 75 11, 15, 3, 7, 27, 31, 19, 23, \ 76 86, 88, 82, 84, 94, 96, 90, 92, \ 77 70, 72, 66, 68, 78, 80, 74, 76, \ 78 127, 127, 127, 127, 127, 127, 127, 127, \ 79 127, 127, 127, 127, 127, 127, 127, 127, \ 80 128, 128, 128, 128, 128, 128, 128, 128, \ 81 128, 128, 128, 128, 128, 128, 128, 128, \ 82 123, 123, 123, 123, 124, 124, 124, 124, \ 83 121, 121, 121, 121, 122, 122, 122, 122, \ 84 126, 126, 126, 126, 126, 126, 126, 126, \ 85 125, 125, 125, 125, 125, 125, 125, 125, \ 86 148, 147, 150, 149, 144, 143, 146, 145, \ 87 156, 155, 158, 157, 152, 151, 154, 153, \ 88 138, 138, 139, 139, 136, 136, 137, 137, \ 89 142, 142, 143, 143, 140, 140, 141, 141, \ 90 213, 209, 221, 217, 197, 193, 205, 201, \ 91 245, 241, 253, 249, 229, 225, 237, 233, \ 92 170, 168, 174, 172, 162, 160, 166, 164, \ 93 186, 184, 190, 188, 178, 176, 182, 180, \ 94 129, 129, 129, 129, 129, 129, 129, 129, \ 95 129, 129, 129, 129, 129, 129, 129, 129, \ 96 128, 128, 128, 128, 128, 128, 128, 128, \ 97 128, 128, 128, 128, 128, 128, 128, 128, \ 98 133, 133, 133, 133, 132, 132, 132, 132, \ 99 135, 135, 135, 135, 134, 134, 134, 134, \ 100 130, 130, 130, 130, 130, 130, 130, 130, \ 101 131, 131, 131, 131, 131, 131, 131, 131, \ 102 } 103 104 #define U8_TO_ULAW { \ 105 0, 0, 0, 0, 0, 1, 1, 1, \ 106 1, 2, 2, 2, 2, 3, 3, 3, \ 107 3, 4, 4, 4, 4, 5, 5, 5, \ 108 5, 6, 6, 6, 6, 7, 7, 7, \ 109 7, 8, 8, 8, 8, 9, 9, 9, \ 110 9, 10, 10, 10, 10, 11, 11, 11, \ 111 11, 12, 12, 12, 12, 13, 13, 13, \ 112 13, 14, 14, 14, 14, 15, 15, 15, \ 113 15, 16, 16, 17, 17, 18, 18, 19, \ 114 19, 20, 20, 21, 21, 22, 22, 23, \ 115 23, 24, 24, 25, 25, 26, 26, 27, \ 116 27, 28, 28, 29, 29, 30, 30, 31, \ 117 31, 32, 33, 34, 35, 36, 37, 38, \ 118 39, 40, 41, 42, 43, 44, 45, 46, \ 119 47, 49, 51, 53, 55, 57, 59, 61, \ 120 63, 66, 70, 74, 78, 84, 92, 104, \ 121 254, 231, 219, 211, 205, 201, 197, 193, \ 122 190, 188, 186, 184, 182, 180, 178, 176, \ 123 175, 174, 173, 172, 171, 170, 169, 168, \ 124 167, 166, 165, 164, 163, 162, 161, 160, \ 125 159, 159, 158, 158, 157, 157, 156, 156, \ 126 155, 155, 154, 154, 153, 153, 152, 152, \ 127 151, 151, 150, 150, 149, 149, 148, 148, \ 128 147, 147, 146, 146, 145, 145, 144, 144, \ 129 143, 143, 143, 143, 142, 142, 142, 142, \ 130 141, 141, 141, 141, 140, 140, 140, 140, \ 131 139, 139, 139, 139, 138, 138, 138, 138, \ 132 137, 137, 137, 137, 136, 136, 136, 136, \ 133 135, 135, 135, 135, 134, 134, 134, 134, \ 134 133, 133, 133, 133, 132, 132, 132, 132, \ 135 131, 131, 131, 131, 130, 130, 130, 130, \ 136 129, 129, 129, 129, 128, 128, 128, 128, \ 137 } 138 139 #define U8_TO_ALAW { \ 140 42, 42, 42, 42, 42, 43, 43, 43, \ 141 43, 40, 40, 40, 40, 41, 41, 41, \ 142 41, 46, 46, 46, 46, 47, 47, 47, \ 143 47, 44, 44, 44, 44, 45, 45, 45, \ 144 45, 34, 34, 34, 34, 35, 35, 35, \ 145 35, 32, 32, 32, 32, 33, 33, 33, \ 146 33, 38, 38, 38, 38, 39, 39, 39, \ 147 39, 36, 36, 36, 36, 37, 37, 37, \ 148 37, 58, 58, 59, 59, 56, 56, 57, \ 149 57, 62, 62, 63, 63, 60, 60, 61, \ 150 61, 50, 50, 51, 51, 48, 48, 49, \ 151 49, 54, 54, 55, 55, 52, 52, 53, \ 152 53, 10, 11, 8, 9, 14, 15, 12, \ 153 13, 2, 3, 0, 1, 6, 7, 4, \ 154 5, 24, 30, 28, 18, 16, 22, 20, \ 155 106, 110, 98, 102, 122, 114, 75, 90, \ 156 213, 197, 245, 253, 229, 225, 237, 233, \ 157 149, 151, 145, 147, 157, 159, 153, 155, \ 158 133, 132, 135, 134, 129, 128, 131, 130, \ 159 141, 140, 143, 142, 137, 136, 139, 138, \ 160 181, 181, 180, 180, 183, 183, 182, 182, \ 161 177, 177, 176, 176, 179, 179, 178, 178, \ 162 189, 189, 188, 188, 191, 191, 190, 190, \ 163 185, 185, 184, 184, 187, 187, 186, 186, \ 164 165, 165, 165, 165, 164, 164, 164, 164, \ 165 167, 167, 167, 167, 166, 166, 166, 166, \ 166 161, 161, 161, 161, 160, 160, 160, 160, \ 167 163, 163, 163, 163, 162, 162, 162, 162, \ 168 173, 173, 173, 173, 172, 172, 172, 172, \ 169 175, 175, 175, 175, 174, 174, 174, 174, \ 170 169, 169, 169, 169, 168, 168, 168, 168, \ 171 171, 171, 171, 171, 170, 170, 170, 170, \ 172 } 173 174 #define _G711_TO_INTPCM(t, v) ((intpcm_t) \ 175 ((int8_t)((t)[(uint8_t)(v)] ^ 0x80))) 176 177 #define _INTPCM_TO_G711(t, v) ((t)[(uint8_t)((v) ^ 0x80)]) 178 179 #define G711_DECLARE_TABLE(t) \ 180 static const struct { \ 181 const uint8_t ulaw_to_u8[G711_TABLE_SIZE]; \ 182 const uint8_t alaw_to_u8[G711_TABLE_SIZE]; \ 183 const uint8_t u8_to_ulaw[G711_TABLE_SIZE]; \ 184 const uint8_t u8_to_alaw[G711_TABLE_SIZE]; \ 185 } t = { \ 186 ULAW_TO_U8, ALAW_TO_U8, \ 187 U8_TO_ULAW, U8_TO_ALAW \ 188 } 189 190 #define G711_DECLARE_OP(t) \ 191 static __inline intpcm_t \ 192 pcm_read_ulaw(uint8_t v) \ 193 { \ 194 \ 195 return (_G711_TO_INTPCM((t).ulaw_to_u8, v)); \ 196 } \ 197 \ 198 static __inline intpcm_t \ 199 pcm_read_alaw(uint8_t v) \ 200 { \ 201 \ 202 return (_G711_TO_INTPCM((t).alaw_to_u8, v)); \ 203 } \ 204 \ 205 static __inline void \ 206 pcm_write_ulaw(uint8_t *dst, intpcm_t v) \ 207 { \ 208 \ 209 *dst = _INTPCM_TO_G711((t).u8_to_ulaw, v); \ 210 } \ 211 \ 212 static __inline void \ 213 pcm_write_alaw(uint8_t *dst, intpcm_t v) \ 214 { \ 215 \ 216 *dst = _INTPCM_TO_G711((t).u8_to_alaw, v); \ 217 } 218 219 #define G711_DECLARE(t) \ 220 G711_DECLARE_TABLE(t); \ 221 G711_DECLARE_OP(t) 222 223 #endif /* !_SND_G711_H_ */ 224