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
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.
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.
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.
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.
22 * Sun Microsystems, Inc.
24 * Mountain View, California 94043
26 /* 16kbps version created, used 24kbps code and changing as little as possible.
27 * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
28 * If any errors are found, please contact me at mrand@tamu.edu
37 * g723_16_encoder(), g723_16_decoder()
39 * These routines comprise an implementation of the CCITT G.726 16 Kbps
40 * ADPCM coding algorithm. Essentially, this implementation is identical to
41 * the bit level description except for a few deviations which take advantage
42 * of workstation attributes, such as hardware 2's complement arithmetic.
47 #include "g72x_priv.h"
50 * Maps G.723_16 code word to reconstructed scale factor normalized log
51 * magnitude values. Comes from Table 11/G.726
53 static short _dqlntab[4] = { 116, 365, 365, 116};
55 /* Maps G.723_16 code word to log of scale factor multiplier.
57 * _witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
58 * as WI << 5 (multiplied by 32), so we'll do that here
60 static short _witab[4] = {-704, 14048, 14048, -704};
63 * Maps G.723_16 code words to a set of values whose long and short
64 * term averages are computed and then compared to give an indication
65 * how stationary (steady state) the signal is.
68 /* Comes from FUNCTF */
69 static short _fitab[4] = {0, 0xE00, 0xE00, 0};
71 /* Comes from quantizer decision level tables (Table 7/G.726)
73 static short qtab_723_16[1] = {261};
79 * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
80 * Returns -1 if invalid input coding value.
85 G72x_STATE *state_ptr)
87 short sei, sezi, se, sez; /* ACCUM */
91 short dqsez; /* ADDC */
94 /* linearize input sample to 14-bit PCM */
95 sl >>= 2; /* sl of 14-bit dynamic range */
97 sezi = predictor_zero(state_ptr);
99 sei = sezi + predictor_pole(state_ptr);
100 se = sei >> 1; /* se = estimated signal */
102 d = sl - se; /* d = estimation diff. */
104 /* quantize prediction difference d */
105 y = step_size(state_ptr); /* quantizer step size */
106 i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
108 /* Since quantize() only produces a three level output
109 * (1, 2, or 3), we must create the fourth one on our own
111 if (i == 3) /* i code for the zero region */
112 if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
115 dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
117 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
119 dqsez = sr + sez - se; /* pole prediction diff. */
121 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
129 * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
130 * the resulting 16-bit linear PCM, A-law or u-law sample value.
131 * -1 is returned if the output coding is unknown.
136 G72x_STATE *state_ptr)
138 short sezi, sei, sez, se; /* ACCUM */
144 i &= 0x03; /* mask to get proper bits */
145 sezi = predictor_zero(state_ptr);
147 sei = sezi + predictor_pole(state_ptr);
148 se = sei >> 1; /* se = estimated signal */
150 y = step_size(state_ptr); /* adaptive quantizer step size */
151 dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
153 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
155 dqsez = sr - se + sez; /* pole prediction diff. */
157 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
159 /* sr was of 14-bit dynamic range */
163 ** Do not edit or modify anything in this comment block.
164 ** The arch-tag line is a file identity tag for the GNU Arch
165 ** revision control system.
167 ** arch-tag: ae265466-c3fc-4f83-bb32-edae488a5ca5