1 /*
2 * This source code is a product of Sun Microsystems, Inc. and is provided
3 * for unrestricted use. Users may copy or modify this source code without
4 * charge.
5 *
6 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9 *
10 * Sun source code is provided with no support and without any obligation on
11 * the part of Sun Microsystems, Inc. to assist in its use, correction,
12 * modification or enhancement.
13 *
14 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16 * OR ANY PART THEREOF.
17 *
18 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19 * or profits or other special, indirect and consequential damages, even if
20 * Sun has been advised of the possibility of such damages.
21 *
22 * Sun Microsystems, Inc.
23 * 2550 Garcia Avenue
24 * Mountain View, California 94043
25 */
26
27 /*
28 * g723_40.c
29 *
30 * Description:
31 *
32 * g723_40_encoder(), g723_40_decoder()
33 *
34 * These routines comprise an implementation of the CCITT G.723 40Kbps
35 * ADPCM coding algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which
37 * take advantage of workstation attributes, such as hardware 2's
38 * complement arithmetic.
39 *
40 * The deviation from the bit level specification (lookup tables),
41 * preserves the bit level performance specifications.
42 *
43 * As outlined in the G.723 Recommendation, the algorithm is broken
44 * down into modules. Each section of code below is preceded by
45 * the name of the module which it is implementing.
46 *
47 */
48 #include "g72x.h"
49
50 /*
51 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
52 * magnitude values.
53 */
54 static short _dqlntab[32] = { -2048, -66, 28, 104, 169, 224, 274, 318, 358, 395, 429,
55 459, 488, 514, 539, 566, 566, 539, 514, 488, 459, 429,
56 395, 358, 318, 274, 224, 169, 104, 28, -66, -2048 };
57
58 /* Maps G.723_40 code word to log of scale factor multiplier. */
59 static short _witab[32] = { 448, 448, 768, 1248, 1280, 1312, 1856, 3200,
60 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
61 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
62 3200, 1856, 1312, 1280, 1248, 768, 448, 448 };
63
64 /*
65 * Maps G.723_40 code words to a set of values whose long and short
66 * term averages are computed and then compared to give an indication
67 * how stationary (steady state) the signal is.
68 */
69 static short _fitab[32] = { 0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
70 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
71 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
72 0x200, 0x200, 0x200, 0, 0, 0, 0, 0 };
73
74 static short qtab_723_40[15] = { -122, -16, 68, 139, 198, 250, 298, 339,
75 378, 413, 445, 475, 502, 528, 553 };
76
77 /*
78 * g723_40_encoder()
79 *
80 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
81 * the resulting 5-bit CCITT G.723 40Kbps code.
82 * Returns -1 if the input coding value is invalid.
83 */
g723_40_encoder(int sl,int in_coding,struct g72x_state * state_ptr)84 int g723_40_encoder(int sl, int in_coding, struct g72x_state* state_ptr)
85 {
86 short sei, sezi, se, sez; /* ACCUM */
87 short d; /* SUBTA */
88 short y; /* MIX */
89 short sr; /* ADDB */
90 short dqsez; /* ADDC */
91 short dq, i;
92
93 switch (in_coding) { /* linearize input sample to 14-bit PCM */
94 case AUDIO_ENCODING_ALAW:
95 sl = alaw2linear(sl) >> 2;
96 break;
97 case AUDIO_ENCODING_ULAW:
98 sl = ulaw2linear(sl) >> 2;
99 break;
100 case AUDIO_ENCODING_LINEAR:
101 sl >>= 2; /* sl of 14-bit dynamic range */
102 break;
103 default:
104 return (-1);
105 }
106
107 sezi = predictor_zero(state_ptr);
108 sez = sezi >> 1;
109 sei = sezi + predictor_pole(state_ptr);
110 se = sei >> 1; /* se = estimated signal */
111
112 d = sl - se; /* d = estimation difference */
113
114 /* quantize prediction difference */
115 y = step_size(state_ptr); /* adaptive quantizer step size */
116 i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
117
118 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */
119
120 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
121
122 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
123
124 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
125
126 return (i);
127 }
128
129 /*
130 * g723_40_decoder()
131 *
132 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
133 * the resulting 16-bit linear PCM, A-law or u-law sample value.
134 * -1 is returned if the output coding is unknown.
135 */
g723_40_decoder(int i,int out_coding,struct g72x_state * state_ptr)136 int g723_40_decoder(int i, int out_coding, struct g72x_state* state_ptr)
137 {
138 short sezi, sei, sez, se; /* ACCUM */
139 short y; /* MIX */
140 short sr; /* ADDB */
141 short dq;
142 short dqsez;
143
144 i &= 0x1f; /* mask to get proper bits */
145 sezi = predictor_zero(state_ptr);
146 sez = sezi >> 1;
147 sei = sezi + predictor_pole(state_ptr);
148 se = sei >> 1; /* se = estimated signal */
149
150 y = step_size(state_ptr); /* adaptive quantizer step size */
151 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */
152
153 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
154
155 dqsez = sr - se + sez; /* pole prediction diff. */
156
157 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
158
159 switch (out_coding) {
160 case AUDIO_ENCODING_ALAW:
161 return (tandem_adjust_alaw(sr, se, y, i, 0x10, qtab_723_40));
162 case AUDIO_ENCODING_ULAW:
163 return (tandem_adjust_ulaw(sr, se, y, i, 0x10, qtab_723_40));
164 case AUDIO_ENCODING_LINEAR:
165 return (sr << 2); /* sr was of 14-bit dynamic range */
166 default:
167 return (-1);
168 }
169 }
170