comparison spandsp-0.0.6pre17/src/g711.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g711.c - A-law and u-law transcoding routines
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2006 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 2.1,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: g711.c,v 1.16 2009/02/10 13:06:46 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if defined(HAVE_CONFIG_H)
31 #include "config.h"
32 #endif
33
34 #include <inttypes.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include "spandsp/telephony.h"
40 #include "spandsp/bit_operations.h"
41 #include "spandsp/g711.h"
42 #include "spandsp/private/g711.h"
43
44 /* Copied from the CCITT G.711 specification */
45 static const uint8_t ulaw_to_alaw_table[256] =
46 {
47 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37,
48 58, 59, 56, 57, 62, 63, 60, 61, 50, 51, 48, 49, 54, 55, 52, 53,
49 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 26,
50 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 106,
51 104, 105, 110, 111, 108, 109, 98, 99, 96, 97, 102, 103, 100, 101, 122, 120,
52 126, 127, 124, 125, 114, 115, 112, 113, 118, 119, 116, 117, 75, 73, 79, 77,
53 66, 67, 64, 65, 70, 71, 68, 69, 90, 91, 88, 89, 94, 95, 92, 93,
54 82, 82, 83, 83, 80, 80, 81, 81, 86, 86, 87, 87, 84, 84, 85, 85,
55 170, 171, 168, 169, 174, 175, 172, 173, 162, 163, 160, 161, 166, 167, 164, 165,
56 186, 187, 184, 185, 190, 191, 188, 189, 178, 179, 176, 177, 182, 183, 180, 181,
57 138, 139, 136, 137, 142, 143, 140, 141, 130, 131, 128, 129, 134, 135, 132, 154,
58 155, 152, 153, 158, 159, 156, 157, 146, 147, 144, 145, 150, 151, 148, 149, 234,
59 232, 233, 238, 239, 236, 237, 226, 227, 224, 225, 230, 231, 228, 229, 250, 248,
60 254, 255, 252, 253, 242, 243, 240, 241, 246, 247, 244, 245, 203, 201, 207, 205,
61 194, 195, 192, 193, 198, 199, 196, 197, 218, 219, 216, 217, 222, 223, 220, 221,
62 210, 210, 211, 211, 208, 208, 209, 209, 214, 214, 215, 215, 212, 212, 213, 213
63 };
64
65 /* These transcoding tables are copied from the CCITT G.711 specification. To achieve
66 optimal results, do not change them. */
67
68 static const uint8_t alaw_to_ulaw_table[256] =
69 {
70 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37,
71 57, 58, 55, 56, 61, 62, 59, 60, 49, 50, 47, 48, 53, 54, 51, 52,
72 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5,
73 26, 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21,
74 98, 99, 96, 97, 102, 103, 100, 101, 93, 93, 92, 92, 95, 95, 94, 94,
75 116, 118, 112, 114, 124, 126, 120, 122, 106, 107, 104, 105, 110, 111, 108, 109,
76 72, 73, 70, 71, 76, 77, 74, 75, 64, 65, 63, 63, 68, 69, 66, 67,
77 86, 87, 84, 85, 90, 91, 88, 89, 79, 79, 78, 78, 82, 83, 80, 81,
78 170, 171, 168, 169, 174, 175, 172, 173, 162, 163, 160, 161, 166, 167, 164, 165,
79 185, 186, 183, 184, 189, 190, 187, 188, 177, 178, 175, 176, 181, 182, 179, 180,
80 138, 139, 136, 137, 142, 143, 140, 141, 130, 131, 128, 129, 134, 135, 132, 133,
81 154, 155, 152, 153, 158, 159, 156, 157, 146, 147, 144, 145, 150, 151, 148, 149,
82 226, 227, 224, 225, 230, 231, 228, 229, 221, 221, 220, 220, 223, 223, 222, 222,
83 244, 246, 240, 242, 252, 254, 248, 250, 234, 235, 232, 233, 238, 239, 236, 237,
84 200, 201, 198, 199, 204, 205, 202, 203, 192, 193, 191, 191, 196, 197, 194, 195,
85 214, 215, 212, 213, 218, 219, 216, 217, 207, 207, 206, 206, 210, 211, 208, 209
86 };
87
88 SPAN_DECLARE(uint8_t) alaw_to_ulaw(uint8_t alaw)
89 {
90 return alaw_to_ulaw_table[alaw];
91 }
92 /*- End of function --------------------------------------------------------*/
93
94 SPAN_DECLARE(uint8_t) ulaw_to_alaw(uint8_t ulaw)
95 {
96 return ulaw_to_alaw_table[ulaw];
97 }
98 /*- End of function --------------------------------------------------------*/
99
100 SPAN_DECLARE(int) g711_decode(g711_state_t *s,
101 int16_t amp[],
102 const uint8_t g711_data[],
103 int g711_bytes)
104 {
105 int i;
106
107 if (s->mode == G711_ALAW)
108 {
109 for (i = 0; i < g711_bytes; i++)
110 amp[i] = alaw_to_linear(g711_data[i]);
111 /*endfor*/
112 }
113 else
114 {
115 for (i = 0; i < g711_bytes; i++)
116 amp[i] = ulaw_to_linear(g711_data[i]);
117 /*endfor*/
118 }
119 /*endif*/
120 return g711_bytes;
121 }
122 /*- End of function --------------------------------------------------------*/
123
124 SPAN_DECLARE(int) g711_encode(g711_state_t *s,
125 uint8_t g711_data[],
126 const int16_t amp[],
127 int len)
128 {
129 int i;
130
131 if (s->mode == G711_ALAW)
132 {
133 for (i = 0; i < len; i++)
134 g711_data[i] = linear_to_alaw(amp[i]);
135 /*endfor*/
136 }
137 else
138 {
139 for (i = 0; i < len; i++)
140 g711_data[i] = linear_to_ulaw(amp[i]);
141 /*endfor*/
142 }
143 /*endif*/
144 return len;
145 }
146 /*- End of function --------------------------------------------------------*/
147
148 SPAN_DECLARE(int) g711_transcode(g711_state_t *s,
149 uint8_t g711_out[],
150 const uint8_t g711_in[],
151 int g711_bytes)
152 {
153 int i;
154
155 if (s->mode == G711_ALAW)
156 {
157 for (i = 0; i < g711_bytes; i++)
158 g711_out[i] = alaw_to_ulaw_table[g711_in[i]];
159 /*endfor*/
160 }
161 else
162 {
163 for (i = 0; i < g711_bytes; i++)
164 g711_out[i] = ulaw_to_alaw_table[g711_in[i]];
165 /*endfor*/
166 }
167 /*endif*/
168 return g711_bytes;
169 }
170 /*- End of function --------------------------------------------------------*/
171
172 SPAN_DECLARE(g711_state_t *) g711_init(g711_state_t *s, int mode)
173 {
174 if (s == NULL)
175 {
176 if ((s = (g711_state_t *) malloc(sizeof(*s))) == NULL)
177 return NULL;
178 }
179 s->mode = mode;
180 return s;
181 }
182 /*- End of function --------------------------------------------------------*/
183
184 SPAN_DECLARE(int) g711_release(g711_state_t *s)
185 {
186 return 0;
187 }
188 /*- End of function --------------------------------------------------------*/
189
190 SPAN_DECLARE(int) g711_free(g711_state_t *s)
191 {
192 free(s);
193 return 0;
194 }
195 /*- End of function --------------------------------------------------------*/
196 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.