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 * g726_24.c
29 *
30 * Description:
31 *
32 * g723_24_encoder(), g723_24_decoder()
33 *
34 * These routines comprise an implementation of the CCITT G.723 24 Kbps
35 * ADPCM coding algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which take advantage
37 * of workstation attributes, such as hardware 2's complement arithmetic.
38 *
39 * The ITU-T G.726 coder is an adaptive differential pulse code modulation
40 * (ADPCM) waveform coding algorithm, suitable for coding of digitized
41 * telephone bandwidth (0.3-3.4 kHz) speech or audio signals sampled at 8 kHz.
42 * This coder operates on a sample-by-sample basis. Input samples may be
43 * represented in linear PCM or companded 8-bit G.711 (m-law/A-law) formats
44 * (i.e., 64 kbps). For 32 kbps operation, each sample is converted into a
45 * 4-bit quantized difference signal resulting in a compression ratio of
46 * 2:1 over the G.711 format. For 24 kbps 40 kbps operation, the quantized
47 * difference signal is 3 bits and 5 bits, respectively.
48 *
49 * $Revision$
50 * $Author$
51 * $Date$
52 */
53 #include "g72x.h"
54 #include "private.h"
55
56 /*
57 * Maps G.723_24 code word to reconstructed scale factor normalized log
58 * magnitude values.
59 */
60 static short _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
61
62 /* Maps G.723_24 code word to log of scale factor multiplier. */
63 static short _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
64
65 /*
66 * Maps G.723_24 code words to a set of values whose long and short
67 * term averages are computed and then compared to give an indication
68 * how stationary (steady state) the signal is.
69 */
70 static short _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
71
72 static int qtab_723_24[3] = {8, 218, 331};
73
74 /*
75 * g723_24_encoder()
76 *
77 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
78 * Returns -1 if invalid input coding value.
79 */
80 int
g726_24_encoder(int sl,int in_coding,g726_state * state_ptr)81 g726_24_encoder(
82 int sl,
83 int in_coding,
84 g726_state *state_ptr)
85 {
86 int sezi;
87 int sei;
88 int sez; /* ACCUM */
89 int se;
90 int d; /* SUBTA */
91 int y; /* MIX */
92 int i;
93 int dq;
94 int sr; /* ADDB */
95 int dqsez; /* ADDC */
96
97 switch (in_coding) { /* linearize input sample to 14-bit PCM */
98 case AUDIO_ENCODING_ALAW:
99 sl = alaw2linear(sl) >> 2;
100 break;
101 case AUDIO_ENCODING_ULAW:
102 sl = ulaw2linear(sl) >> 2;
103 break;
104 case AUDIO_ENCODING_LINEAR:
105 sl >>= 2; /* sl of 14-bit dynamic range */
106 break;
107 default:
108 return (-1);
109 }
110
111 sezi = predictor_zero(state_ptr);
112 sez = sezi >> 1;
113 sei = sezi + predictor_pole(state_ptr);
114 se = sei >> 1; /* se = estimated signal */
115
116 d = sl - se; /* d = estimation diff. */
117
118 /* quantize prediction difference d */
119 y = step_size(state_ptr); /* quantizer step size */
120 i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */
121 dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
122
123 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
124
125 dqsez = sr + sez - se; /* pole prediction diff. */
126
127 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
128
129 return (i);
130 }
131
132 /*
133 * g723_24_decoder()
134 *
135 * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
136 * the resulting 16-bit linear PCM, A-law or u-law sample value.
137 * -1 is returned if the output coding is unknown.
138 */
139 int
g726_24_decoder(int i,int out_coding,g726_state * state_ptr)140 g726_24_decoder(
141 int i,
142 int out_coding,
143 g726_state *state_ptr)
144 {
145 int sezi;
146 int sez; /* ACCUM */
147 int sei;
148 int se;
149 int y; /* MIX */
150 int dq;
151 int sr; /* ADDB */
152 int dqsez;
153
154 i &= 0x07; /* mask to get proper bits */
155 sezi = predictor_zero(state_ptr);
156 sez = sezi >> 1;
157 sei = sezi + predictor_pole(state_ptr);
158 se = sei >> 1; /* se = estimated signal */
159
160 y = step_size(state_ptr); /* adaptive quantizer step size */
161 dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
162
163 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
164
165 dqsez = sr - se + sez; /* pole prediction diff. */
166
167 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
168
169 switch (out_coding) {
170 case AUDIO_ENCODING_ALAW:
171 return (tandem_adjust_alaw(sr, se, y, i, 4, qtab_723_24));
172 case AUDIO_ENCODING_ULAW:
173 return (tandem_adjust_ulaw(sr, se, y, i, 4, qtab_723_24));
174 case AUDIO_ENCODING_LINEAR:
175 return (sr << 2); /* sr was of 14-bit dynamic range */
176 default:
177 return (-1);
178 }
179 }
180
181