Chris@0
|
1 /*
|
Chris@0
|
2 * This source code is a product of Sun Microsystems, Inc. and is provided
|
Chris@0
|
3 * for unrestricted use. Users may copy or modify this source code without
|
Chris@0
|
4 * charge.
|
Chris@0
|
5 *
|
Chris@0
|
6 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
|
Chris@0
|
7 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
Chris@0
|
8 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
|
Chris@0
|
9 *
|
Chris@0
|
10 * Sun source code is provided with no support and without any obligation on
|
Chris@0
|
11 * the part of Sun Microsystems, Inc. to assist in its use, correction,
|
Chris@0
|
12 * modification or enhancement.
|
Chris@0
|
13 *
|
Chris@0
|
14 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
|
Chris@0
|
15 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
|
Chris@0
|
16 * OR ANY PART THEREOF.
|
Chris@0
|
17 *
|
Chris@0
|
18 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
|
Chris@0
|
19 * or profits or other special, indirect and consequential damages, even if
|
Chris@0
|
20 * Sun has been advised of the possibility of such damages.
|
Chris@0
|
21 *
|
Chris@0
|
22 * Sun Microsystems, Inc.
|
Chris@0
|
23 * 2550 Garcia Avenue
|
Chris@0
|
24 * Mountain View, California 94043
|
Chris@0
|
25 */
|
Chris@0
|
26 /* 16kbps version created, used 24kbps code and changing as little as possible.
|
Chris@0
|
27 * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
|
Chris@0
|
28 * If any errors are found, please contact me at mrand@tamu.edu
|
Chris@0
|
29 * -Marc Randolph
|
Chris@0
|
30 */
|
Chris@0
|
31
|
Chris@0
|
32 /*
|
Chris@0
|
33 * g723_16.c
|
Chris@0
|
34 *
|
Chris@0
|
35 * Description:
|
Chris@0
|
36 *
|
Chris@0
|
37 * g723_16_encoder(), g723_16_decoder()
|
Chris@0
|
38 *
|
Chris@0
|
39 * These routines comprise an implementation of the CCITT G.726 16 Kbps
|
Chris@0
|
40 * ADPCM coding algorithm. Essentially, this implementation is identical to
|
Chris@0
|
41 * the bit level description except for a few deviations which take advantage
|
Chris@0
|
42 * of workstation attributes, such as hardware 2's complement arithmetic.
|
Chris@0
|
43 *
|
Chris@0
|
44 */
|
Chris@0
|
45
|
Chris@0
|
46 #include "g72x.h"
|
Chris@0
|
47 #include "g72x_priv.h"
|
Chris@0
|
48
|
Chris@0
|
49 /*
|
Chris@0
|
50 * Maps G.723_16 code word to reconstructed scale factor normalized log
|
Chris@0
|
51 * magnitude values. Comes from Table 11/G.726
|
Chris@0
|
52 */
|
Chris@0
|
53 static short _dqlntab[4] = { 116, 365, 365, 116};
|
Chris@0
|
54
|
Chris@0
|
55 /* Maps G.723_16 code word to log of scale factor multiplier.
|
Chris@0
|
56 *
|
Chris@0
|
57 * _witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
|
Chris@0
|
58 * as WI << 5 (multiplied by 32), so we'll do that here
|
Chris@0
|
59 */
|
Chris@0
|
60 static short _witab[4] = {-704, 14048, 14048, -704};
|
Chris@0
|
61
|
Chris@0
|
62 /*
|
Chris@0
|
63 * Maps G.723_16 code words to a set of values whose long and short
|
Chris@0
|
64 * term averages are computed and then compared to give an indication
|
Chris@0
|
65 * how stationary (steady state) the signal is.
|
Chris@0
|
66 */
|
Chris@0
|
67
|
Chris@0
|
68 /* Comes from FUNCTF */
|
Chris@0
|
69 static short _fitab[4] = {0, 0xE00, 0xE00, 0};
|
Chris@0
|
70
|
Chris@0
|
71 /* Comes from quantizer decision level tables (Table 7/G.726)
|
Chris@0
|
72 */
|
Chris@0
|
73 static short qtab_723_16[1] = {261};
|
Chris@0
|
74
|
Chris@0
|
75
|
Chris@0
|
76 /*
|
Chris@0
|
77 * g723_16_encoder()
|
Chris@0
|
78 *
|
Chris@0
|
79 * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
|
Chris@0
|
80 * Returns -1 if invalid input coding value.
|
Chris@0
|
81 */
|
Chris@0
|
82 int
|
Chris@0
|
83 g723_16_encoder(
|
Chris@0
|
84 int sl,
|
Chris@0
|
85 G72x_STATE *state_ptr)
|
Chris@0
|
86 {
|
Chris@0
|
87 short sei, sezi, se, sez; /* ACCUM */
|
Chris@0
|
88 short d; /* SUBTA */
|
Chris@0
|
89 short y; /* MIX */
|
Chris@0
|
90 short sr; /* ADDB */
|
Chris@0
|
91 short dqsez; /* ADDC */
|
Chris@0
|
92 short dq, i;
|
Chris@0
|
93
|
Chris@0
|
94 /* linearize input sample to 14-bit PCM */
|
Chris@0
|
95 sl >>= 2; /* sl of 14-bit dynamic range */
|
Chris@0
|
96
|
Chris@0
|
97 sezi = predictor_zero(state_ptr);
|
Chris@0
|
98 sez = sezi >> 1;
|
Chris@0
|
99 sei = sezi + predictor_pole(state_ptr);
|
Chris@0
|
100 se = sei >> 1; /* se = estimated signal */
|
Chris@0
|
101
|
Chris@0
|
102 d = sl - se; /* d = estimation diff. */
|
Chris@0
|
103
|
Chris@0
|
104 /* quantize prediction difference d */
|
Chris@0
|
105 y = step_size(state_ptr); /* quantizer step size */
|
Chris@0
|
106 i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
|
Chris@0
|
107
|
Chris@0
|
108 /* Since quantize() only produces a three level output
|
Chris@0
|
109 * (1, 2, or 3), we must create the fourth one on our own
|
Chris@0
|
110 */
|
Chris@0
|
111 if (i == 3) /* i code for the zero region */
|
Chris@0
|
112 if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
|
Chris@0
|
113 i = 0;
|
Chris@0
|
114
|
Chris@0
|
115 dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
|
Chris@0
|
116
|
Chris@0
|
117 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
|
Chris@0
|
118
|
Chris@0
|
119 dqsez = sr + sez - se; /* pole prediction diff. */
|
Chris@0
|
120
|
Chris@0
|
121 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
|
Chris@0
|
122
|
Chris@0
|
123 return (i);
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /*
|
Chris@0
|
127 * g723_16_decoder()
|
Chris@0
|
128 *
|
Chris@0
|
129 * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
|
Chris@0
|
130 * the resulting 16-bit linear PCM, A-law or u-law sample value.
|
Chris@0
|
131 * -1 is returned if the output coding is unknown.
|
Chris@0
|
132 */
|
Chris@0
|
133 int
|
Chris@0
|
134 g723_16_decoder(
|
Chris@0
|
135 int i,
|
Chris@0
|
136 G72x_STATE *state_ptr)
|
Chris@0
|
137 {
|
Chris@0
|
138 short sezi, sei, sez, se; /* ACCUM */
|
Chris@0
|
139 short y; /* MIX */
|
Chris@0
|
140 short sr; /* ADDB */
|
Chris@0
|
141 short dq;
|
Chris@0
|
142 short dqsez;
|
Chris@0
|
143
|
Chris@0
|
144 i &= 0x03; /* mask to get proper bits */
|
Chris@0
|
145 sezi = predictor_zero(state_ptr);
|
Chris@0
|
146 sez = sezi >> 1;
|
Chris@0
|
147 sei = sezi + predictor_pole(state_ptr);
|
Chris@0
|
148 se = sei >> 1; /* se = estimated signal */
|
Chris@0
|
149
|
Chris@0
|
150 y = step_size(state_ptr); /* adaptive quantizer step size */
|
Chris@0
|
151 dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
|
Chris@0
|
152
|
Chris@0
|
153 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
|
Chris@0
|
154
|
Chris@0
|
155 dqsez = sr - se + sez; /* pole prediction diff. */
|
Chris@0
|
156
|
Chris@0
|
157 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
|
Chris@0
|
158
|
Chris@0
|
159 /* sr was of 14-bit dynamic range */
|
Chris@0
|
160 return (sr << 2);
|
Chris@0
|
161 }
|
Chris@0
|
162
|