Debian Codegraph @

src/speech/g723_40.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2010 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: March 2001 */
35 /*************************************************************************/
36 /* */
37 /* Minor fixes to make it properly ansi and fit into cst */
38 /* */
39 /*************************************************************************/
40 /*
41 * This source code is a product of Sun Microsystems, Inc. and is provided
42 * for unrestricted use. Users may copy or modify this source code without
43 * charge.
44 *
45 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
46 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
47 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
48 *
49 * Sun source code is provided with no support and without any obligation on
50 * the part of Sun Microsystems, Inc. to assist in its use, correction,
51 * modification or enhancement.
52 *
53 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
54 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
55 * OR ANY PART THEREOF.
56 *
57 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
58 * or profits or other special, indirect and consequential damages, even if
59 * Sun has been advised of the possibility of such damages.
60 *
61 * Sun Microsystems, Inc.
62 * 2550 Garcia Avenue
63 * Mountain View, California 94043
64 */
65
66 /*
67 * g723_40.c
68 *
69 * Description:
70 *
71 * g723_40_encoder(), g723_40_decoder()
72 *
73 * These routines comprise an implementation of the CCITT G.723 40Kbps
74 * ADPCM coding algorithm. Essentially, this implementation is identical to
75 * the bit level description except for a few deviations which
76 * take advantage of workstation attributes, such as hardware 2's
77 * complement arithmetic.
78 *
79 * The deviation from the bit level specification (lookup tables),
80 * preserves the bit level performance specifications.
81 *
82 * As outlined in the G.723 Recommendation, the algorithm is broken
83 * down into modules. Each section of code below is preceded by
84 * the name of the module which it is implementing.
85 *
86 */
87 #include "g72x.h"
88
89 /*
90 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
91 * magnitude values.
92 */
93 static short _dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
94 358, 395, 429, 459, 488, 514, 539, 566,
95 566, 539, 514, 488, 459, 429, 395, 358,
96 318, 274, 224, 169, 104, 28, -66, -2048};
97
98 /* Maps G.723_40 code word to log of scale factor multiplier. */
99 static short _witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
100 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
101 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
102 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
103
104 /*
105 * Maps G.723_40 code words to a set of values whose long and short
106 * term averages are computed and then compared to give an indication
107 * how stationary (steady state) the signal is.
108 */
109 static short _fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
110 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
111 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
112 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
113
114 static short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
115 378, 413, 445, 475, 502, 528, 553};
116
117 /*
118 * g723_40_encoder()
119 *
120 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
121 * the resulting 5-bit CCITT G.723 40Kbps code.
122 * Returns -1 if the input coding value is invalid.
123 */
124 int
125 g723_40_encoder(
126 int sl,
127 int in_coding,
128 struct g72x_state *state_ptr)
129 {
130 short sei, sezi, se, sez; /* ACCUM */
131 short d; /* SUBTA */
132 short y; /* MIX */
133 short sr; /* ADDB */
134 short dqsez; /* ADDC */
135 short dq, i;
136
137 switch (in_coding) { /* linearize input sample to 14-bit PCM */
138 case AUDIO_ENCODING_LINEAR:
139 sl >>= 2; /* sl of 14-bit dynamic range */
140 break;
141 default:
142 return (-1);
143 }
144
145 sezi = g72x_predictor_zero(state_ptr);
146 sez = sezi >> 1;
147 sei = sezi + g72x_predictor_pole(state_ptr);
148 se = sei >> 1; /* se = estimated signal */
149
150 d = sl - se; /* d = estimation difference */
151
152 /* quantize prediction difference */
153 y = g72x_step_size(state_ptr); /* adaptive quantizer step size */
154 i = g72x_quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
155
156 dq = g72x_reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */
157
158 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
159
160 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
161
162 g72x_update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
163
164 return (i);
165 }
166
167 /*
168 * g723_40_decoder()
169 *
170 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
171 * the resulting 16-bit linear PCM, A-law or u-law sample value.
172 * -1 is returned if the output coding is unknown.
173 */
174 int
175 g723_40_decoder(
176 int i,
177 int out_coding,
178 struct g72x_state *state_ptr)
179 {
180 short sezi, sei, sez, se; /* ACCUM */
181 short y; /* MIX */
182 short sr; /* ADDB */
183 short dq;
184 short dqsez;
185
186 i &= 0x1f; /* mask to get proper bits */
187 sezi = g72x_predictor_zero(state_ptr);
188 sez = sezi >> 1;
189 sei = sezi + g72x_predictor_pole(state_ptr);
190 se = sei >> 1; /* se = estimated signal */
191
192 y = g72x_step_size(state_ptr); /* adaptive quantizer step size */
193 dq = g72x_reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */
194
195 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
196
197 dqsez = sr - se + sez; /* pole prediction diff. */
198
199 g72x_update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
200
201 switch (out_coding) {
202 case AUDIO_ENCODING_LINEAR:
203 return (sr << 2); /* sr was of 14-bit dynamic range */
204 default:
205 return (-1);
206 }
207 }