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
32 * g721_encoder(), g721_decoder()
34 * These routines comprise an implementation of the CCITT G.721 ADPCM
35 * coding algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which
37 * take advantage of work station attributes, such as hardware 2's
38 * complement arithmetic and large memory. Specifically, certain time
39 * consuming operations such as multiplications are replaced
40 * with lookup tables and software 2's complement operations are
41 * replaced with hardware 2's complement.
43 * The deviation from the bit level specification (lookup tables)
44 * preserves the bit level performance specifications.
46 * As outlined in the G.721 Recommendation, the algorithm is broken
47 * down into modules. Each section of code below is preceded by
48 * the name of the module which it is implementing.
53 #include "g72x_priv.h"
55 static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
57 * Maps G.721 code word to reconstructed scale factor normalized log
60 static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
61 425, 373, 323, 273, 213, 135, 4, -2048};
63 /* Maps G.721 code word to log of scale factor multiplier. */
64 static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
65 1122, 355, 198, 112, 64, 41, 18, -12};
67 * Maps G.721 code words to a set of values whose long and short
68 * term averages are computed and then compared to give an indication
69 * how stationary (steady state) the signal is.
71 static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
72 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
77 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
78 * the resulting code. -1 is returned for unknown input coding value.
83 G72x_STATE *state_ptr)
85 short sezi, se, sez; /* ACCUM */
89 short dqsez; /* ADDC */
92 /* linearize input sample to 14-bit PCM */
93 sl >>= 2; /* 14-bit dynamic range */
95 sezi = predictor_zero(state_ptr);
97 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
99 d = sl - se; /* estimation difference */
101 /* quantize the prediction difference */
102 y = step_size(state_ptr); /* quantizer step size */
103 i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
105 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
107 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
109 dqsez = sr + sez - se; /* pole prediction diff. */
111 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
121 * Decodes a 4-bit code of G.721 encoded data of i and
122 * returns the resulting linear PCM, A-law or u-law value.
123 * return -1 for unknown out_coding value.
128 G72x_STATE *state_ptr)
130 short sezi, sei, sez, se; /* ACCUM */
136 i &= 0x0f; /* mask to get proper bits */
137 sezi = predictor_zero(state_ptr);
139 sei = sezi + predictor_pole(state_ptr);
140 se = sei >> 1; /* se = estimated signal */
142 y = step_size(state_ptr); /* dynamic quantizer step size */
144 dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
146 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
148 dqsez = sr - se + sez; /* pole prediction diff. */
150 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
152 /* sr was 14-bit dynamic range */
156 ** Do not edit or modify anything in this comment block.
157 ** The arch-tag line is a file identity tag for the GNU Arch
158 ** revision control system.
160 ** arch-tag: 101b6e25-457d-490a-99ae-e2e74a26ea24