• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* 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
29  *      -Marc Randolph
30  */
31 
32 /*
33  * g723_16.c
34  *
35  * Description:
36  *
37  * g723_16_encoder (), g723_16_decoder ()
38  *
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.
43  *
44  */
45 
46 #include "g72x.h"
47 #include "g72x_priv.h"
48 
49 /*
50  * Maps G.723_16 code word to reconstructed scale factor normalized log
51  * magnitude values.  Comes from Table 11/G.726
52  */
53 static short _dqlntab [4] = { 116, 365, 365, 116 } ;
54 
55 /* Maps G.723_16 code word to log of scale factor multiplier.
56  *
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
59  */
60 static short _witab [4] = { -704, 14048, 14048, -704 } ;
61 
62 /*
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.
66  */
67 
68 /* Comes from FUNCTF */
69 static short _fitab [4] = { 0, 0xE00, 0xE00, 0 } ;
70 
71 /* Comes from quantizer decision level tables (Table 7/G.726)
72  */
73 static short qtab_723_16 [1] = { 261 } ;
74 
75 
76 /*
77  * g723_16_encoder ()
78  *
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.
81  */
82 int
g723_16_encoder(int sl,G72x_STATE * state_ptr)83 g723_16_encoder (
84 	int			sl,
85 	G72x_STATE *state_ptr)
86 {
87 	short sei, sezi, se, sez ;	/* ACCUM */
88 	short d ;					/* SUBTA */
89 	short y ;					/* MIX */
90 	short sr ;					/* ADDB */
91 	short dqsez ;				/* ADDC */
92 	short dq, i ;
93 
94 	/* linearize input sample to 14-bit PCM */
95 	sl >>= 2 ;	/* sl of 14-bit dynamic range */
96 
97 	sezi = predictor_zero (state_ptr) ;
98 	sez = sezi >> 1 ;
99 	sei = sezi + predictor_pole (state_ptr) ;
100 	se = sei >> 1 ;	/* se = estimated signal */
101 
102 	d = sl - se ;	/* d = estimation diff. */
103 
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 */
107 
108 	/* Since quantize () only produces a three level output
109 	 * (1, 2, or 3), we must create the fourth one on our own
110 	 */
111 	if (i == 3)					/* i code for the zero region */
112 		if ((d & 0x8000) == 0)	/* If d > 0, i=3 isn't right... */
113 			i = 0 ;
114 
115 	dq = reconstruct (i & 2, _dqlntab [i], y) ; /* quantized diff. */
116 
117 	sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq ; /* reconstructed signal */
118 
119 	dqsez = sr + sez - se ;		/* pole prediction diff. */
120 
121 	update (2, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
122 
123 	return i ;
124 }
125 
126 /*
127  * g723_16_decoder ()
128  *
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.
132  */
133 int
g723_16_decoder(int i,G72x_STATE * state_ptr)134 g723_16_decoder (
135 	int			i,
136 	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 &= 0x03 ;			/* 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 & 0x02, _dqlntab [i], y) ; /* unquantize pred diff */
152 
153 	sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq) ; /* reconst. signal */
154 
155 	dqsez = sr - se + sez ;	/* pole prediction diff. */
156 
157 	update (2, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
158 
159 	/* sr was of 14-bit dynamic range */
160 	return (sr << 2) ;
161 }
162 
163