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_32.c
29  *
30  * Description:
31  *
32  * g721_encoder(), g721_decoder()
33  *
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.
42  *
43  * The deviation from the bit level specification (lookup tables)
44  * preserves the bit level performance specifications.
45  *
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.
49  *
50  * The ITU-T G.726 coder is an adaptive differential pulse code modulation
51  * (ADPCM) waveform coding algorithm, suitable for coding of digitized
52  * telephone bandwidth (0.3-3.4 kHz) speech or audio signals sampled at 8 kHz.
53  * This coder operates on a sample-by-sample basis. Input samples may be
54  * represented in linear PCM or companded 8-bit G.711 (m-law/A-law) formats
55  * (i.e., 64 kbps). For 32 kbps operation, each sample is converted into a
56  * 4-bit quantized difference signal resulting in a compression ratio of
57  * 2:1 over the G.711 format. For 24 kbps 40 kbps operation, the quantized
58  * difference signal is 3 bits and 5 bits, respectively.
59  *
60  * $Revision: 22678 $
61  * $Author: rjongbloed $
62  * $Date: 2009-05-20 19:35:59 -0500 (Wed, 20 May 2009) $
63  */
64 #include "g72x.h"
65 #include "private.h"
66 
67 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
68 /*
69  * Maps G.721 code word to reconstructed scale factor normalized log
70  * magnitude values.
71  */
72 static short	_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
73 				425, 373, 323, 273, 213, 135, 4, -2048};
74 
75 /* Maps G.721 code word to log of scale factor multiplier. */
76 static short	_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
77 				1122, 355, 198, 112, 64, 41, 18, -12};
78 /*
79  * Maps G.721 code words to a set of values whose long and short
80  * term averages are computed and then compared to give an indication
81  * how stationary (steady state) the signal is.
82  */
83 static short	_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
84 				0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
85 
86 /*
87  * g721_encoder()
88  *
89  * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
90  * the resulting code. -1 is returned for unknown input coding value.
91  */
92 int
g726_32_encoder(int sl,int in_coding,g726_state * state_ptr)93 g726_32_encoder(
94 	int		sl,
95 	int		in_coding,
96 	g726_state *state_ptr)
97 {
98 	int		sezi;
99 	int		sez;			/* ACCUM */
100 	int		se;
101 	int		d;				/* SUBTA */
102 	int		y;				/* MIX */
103 	int		i;
104 	int		dq;
105 	int		sr;				/* ADDB */
106 	int		dqsez;			/* ADDC */
107 
108 	switch (in_coding) {	/* linearize input sample to 14-bit PCM */
109 	case AUDIO_ENCODING_ALAW:
110 		sl = alaw2linear(sl) >> 2;
111 		break;
112 	case AUDIO_ENCODING_ULAW:
113 		sl = ulaw2linear(sl) >> 2;
114 		break;
115 	case AUDIO_ENCODING_LINEAR:
116 		sl >>= 2;			/* 14-bit dynamic range */
117 		break;
118 	default:
119 		return (-1);
120 	}
121 
122 	sezi = predictor_zero(state_ptr);
123 	sez = sezi >> 1;
124 	se = (sezi + predictor_pole(state_ptr)) >> 1;	/* estimated signal */
125 
126 	d = sl - se;				/* estimation difference */
127 
128 	/* quantize the prediction difference */
129 	y = step_size(state_ptr);		/* quantizer step size */
130 	i = quantize(d, y, qtab_721, 7);	/* i = ADPCM code */
131 
132 	dq = reconstruct(i & 8, _dqlntab[i], y);	/* quantized est diff */
133 
134 	sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;	/* reconst. signal */
135 
136 	dqsez = sr + sez - se;			/* pole prediction diff. */
137 
138 	update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
139 
140 	return (i);
141 }
142 
143 /*
144  * g721_decoder()
145  *
146  * Description:
147  *
148  * Decodes a 4-bit code of G.721 encoded data of i and
149  * returns the resulting linear PCM, A-law or u-law value.
150  * return -1 for unknown out_coding value.
151  */
152 int
g726_32_decoder(int i,int out_coding,g726_state * state_ptr)153 g726_32_decoder(
154 	int		i,
155 	int		out_coding,
156 	g726_state *state_ptr)
157 {
158 	int		sezi;
159 	int		sez;			/* ACCUM */
160 	int		sei;
161 	int		se;
162 	int		y;				/* MIX */
163 	int		dq;
164 	int		sr;				/* ADDB */
165 	int		dqsez;
166 	long	lino;
167 
168 	i &= 0x0f;				/* mask to get proper bits */
169 	sezi = predictor_zero(state_ptr);
170 	sez = sezi >> 1;
171 	sei = sezi + predictor_pole(state_ptr);
172 	se = sei >> 1;			/* se = estimated signal */
173 
174 	y = step_size(state_ptr);	/* dynamic quantizer step size */
175 
176 	dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
177 
178 	sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq;	/* reconst. signal */
179 
180 	dqsez = sr - se + sez;			/* pole prediction diff. */
181 
182 	update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
183 
184 	switch (out_coding) {
185 	case AUDIO_ENCODING_ALAW:
186 		return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721));
187 	case AUDIO_ENCODING_ULAW:
188 		return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721));
189 	case AUDIO_ENCODING_LINEAR:
190         lino = (long)sr << 2;  /* this seems to overflow a short*/
191 		lino = lino > 32767 ? 32767 : lino;
192 		lino = lino < -32768 ? -32768 : lino;
193 		return lino;//(sr << 2);	/* sr was 14-bit dynamic range */
194 	default:
195 		return (-1);
196 	}
197 }
198 
199