xref: /onnv-gate/usr/src/cmd/audio/utilities/g721.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * Description:
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * g721_encode(), g721_decode(), g721_set_law()
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * These routines comprise an implementation of the CCITT G.721 ADPCM coding
36*0Sstevel@tonic-gate  * algorithm.  Essentially, this implementation is identical to
37*0Sstevel@tonic-gate  * the bit level description except for a few deviations which
38*0Sstevel@tonic-gate  * take advantage of work station attributes, such as hardware 2's
39*0Sstevel@tonic-gate  * complement arithmetic and large memory. Specifically, certain time
40*0Sstevel@tonic-gate  * consuming operations such as multiplications are replaced
41*0Sstevel@tonic-gate  * with look up tables and software 2's complement operations are
42*0Sstevel@tonic-gate  * replaced with hardware 2's complement.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * The deviation (look up tables) from the bit level
45*0Sstevel@tonic-gate  * specification, preserves the bit level performance specifications.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * As outlined in the G.721 Recommendation, the algorithm is broken
48*0Sstevel@tonic-gate  * down into modules.  Each section of code below is preceded by
49*0Sstevel@tonic-gate  * the name of the module which it is implementing.
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate #include <stdlib.h>
53*0Sstevel@tonic-gate #include <libaudio.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Maps G.721 code word to reconstructed scale factor normalized log
57*0Sstevel@tonic-gate  * magnitude values.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate static short	_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
60*0Sstevel@tonic-gate 		    425, 373, 323, 273, 213, 135, 4, -2048};
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /* Maps G.721 code word to log of scale factor multiplier. */
63*0Sstevel@tonic-gate static long	_witab[16] = {-384, 576, 1312, 2048, 3584, 6336, 11360, 35904,
64*0Sstevel@tonic-gate 		    35904, 11360, 6336, 3584, 2048, 1312, 576, -384};
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * Maps G.721 code words to a set of values whose long and short
68*0Sstevel@tonic-gate  * term averages are computed and then compared to give an indication
69*0Sstevel@tonic-gate  * how stationary (steady state) the signal is.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static short	_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
72*0Sstevel@tonic-gate 		    0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * g721_init_state()
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  * Description:
78*0Sstevel@tonic-gate  *
79*0Sstevel@tonic-gate  * This routine initializes and/or resets the audio_g72x_state structure
80*0Sstevel@tonic-gate  * pointed to by 'state_ptr'.
81*0Sstevel@tonic-gate  * All the initial state values are specified in the G.721 standard specs.
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate void
g721_init_state(struct audio_g72x_state * state_ptr)84*0Sstevel@tonic-gate g721_init_state(
85*0Sstevel@tonic-gate 	struct audio_g72x_state *state_ptr)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	int cnta;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	state_ptr->yl = 34816;
90*0Sstevel@tonic-gate 	state_ptr->yu = 544;
91*0Sstevel@tonic-gate 	state_ptr->dms = 0;
92*0Sstevel@tonic-gate 	state_ptr->dml = 0;
93*0Sstevel@tonic-gate 	state_ptr->ap = 0;
94*0Sstevel@tonic-gate 	for (cnta = 0; cnta < 2; cnta++) {
95*0Sstevel@tonic-gate 		state_ptr->a[cnta] = 0;
96*0Sstevel@tonic-gate 		state_ptr->pk[cnta] = 0;
97*0Sstevel@tonic-gate 		state_ptr->sr[cnta] = 32;
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 	for (cnta = 0; cnta < 6; cnta++) {
100*0Sstevel@tonic-gate 		state_ptr->b[cnta] = 0;
101*0Sstevel@tonic-gate 		state_ptr->dq[cnta] = 32;
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	state_ptr->td = 0;
104*0Sstevel@tonic-gate 	state_ptr->leftover_cnt = 0;		/* no left over codes */
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * _g721_fmult()
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  * returns the integer product of the "floating point" an and srn
111*0Sstevel@tonic-gate  * by the lookup table _fmultwanmant[].
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate static int
_g721_fmult(int an,int srn)115*0Sstevel@tonic-gate _g721_fmult(
116*0Sstevel@tonic-gate 	int	an,
117*0Sstevel@tonic-gate 	int	srn)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	short	anmag, anexp, anmant;
120*0Sstevel@tonic-gate 	short	wanexp;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (an == 0) {
123*0Sstevel@tonic-gate 		return ((srn >= 0) ?
124*0Sstevel@tonic-gate 		    ((srn & 077) + 1) >> (18 - (srn >> 6)) :
125*0Sstevel@tonic-gate 		    -(((srn & 077) + 1) >> (2 - (srn >> 6))));
126*0Sstevel@tonic-gate 	} else if (an > 0) {
127*0Sstevel@tonic-gate 		anexp = _fmultanexp[an] - 12;
128*0Sstevel@tonic-gate 		anmant = ((anexp >= 0) ? an >> anexp : an << -anexp) & 07700;
129*0Sstevel@tonic-gate 		if (srn >= 0) {
130*0Sstevel@tonic-gate 			wanexp = anexp + (srn >> 6) - 7;
131*0Sstevel@tonic-gate 			return ((wanexp >= 0) ?
132*0Sstevel@tonic-gate 			    (_fmultwanmant[(srn & 077) + anmant] << wanexp)
133*0Sstevel@tonic-gate 			    & 0x7FFF :
134*0Sstevel@tonic-gate 			    _fmultwanmant[(srn & 077) + anmant] >> -wanexp);
135*0Sstevel@tonic-gate 		} else {
136*0Sstevel@tonic-gate 			wanexp = anexp + (srn >> 6) - 0xFFF7;
137*0Sstevel@tonic-gate 			return ((wanexp >= 0) ?
138*0Sstevel@tonic-gate 			    -((_fmultwanmant[(srn & 077) + anmant] << wanexp)
139*0Sstevel@tonic-gate 			    & 0x7FFF) :
140*0Sstevel@tonic-gate 			    -(_fmultwanmant[(srn & 077) + anmant] >> -wanexp));
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 	} else {
143*0Sstevel@tonic-gate 		anmag = (-an) & 0x1FFF;
144*0Sstevel@tonic-gate 		anexp = _fmultanexp[anmag] - 12;
145*0Sstevel@tonic-gate 		anmant = ((anexp >= 0) ? anmag >> anexp : anmag << -anexp)
146*0Sstevel@tonic-gate 		    & 07700;
147*0Sstevel@tonic-gate 		if (srn >= 0) {
148*0Sstevel@tonic-gate 			wanexp = anexp + (srn >> 6) - 7;
149*0Sstevel@tonic-gate 			return ((wanexp >= 0) ?
150*0Sstevel@tonic-gate 			    -((_fmultwanmant[(srn & 077) + anmant] << wanexp)
151*0Sstevel@tonic-gate 			    & 0x7FFF) :
152*0Sstevel@tonic-gate 			    -(_fmultwanmant[(srn & 077) + anmant] >> -wanexp));
153*0Sstevel@tonic-gate 		} else {
154*0Sstevel@tonic-gate 			wanexp = anexp + (srn >> 6) - 0xFFF7;
155*0Sstevel@tonic-gate 			return ((wanexp >= 0) ?
156*0Sstevel@tonic-gate 			    (_fmultwanmant[(srn & 077) + anmant] << wanexp)
157*0Sstevel@tonic-gate 			    & 0x7FFF :
158*0Sstevel@tonic-gate 			    _fmultwanmant[(srn & 077) + anmant] >> -wanexp);
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * _g721_update()
165*0Sstevel@tonic-gate  *
166*0Sstevel@tonic-gate  * updates the state variables for each output code
167*0Sstevel@tonic-gate  *
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate static void
_g721_update(int y,int i,int dq,int sr,int pk0,struct audio_g72x_state * state_ptr,int sigpk)170*0Sstevel@tonic-gate _g721_update(
171*0Sstevel@tonic-gate 	int	y,
172*0Sstevel@tonic-gate 	int	i,
173*0Sstevel@tonic-gate 	int	dq,
174*0Sstevel@tonic-gate 	int	sr,
175*0Sstevel@tonic-gate 	int	pk0,
176*0Sstevel@tonic-gate 	struct audio_g72x_state *state_ptr,
177*0Sstevel@tonic-gate 	int	sigpk)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	int	cnt;
180*0Sstevel@tonic-gate 	long	fi;				/* FUNCTF */
181*0Sstevel@tonic-gate 	short	mag, exp;			/* FLOAT A */
182*0Sstevel@tonic-gate 	short	a2p;				/* LIMC */
183*0Sstevel@tonic-gate 	short	a1ul;				/* UPA1 */
184*0Sstevel@tonic-gate 	short	pks1, fa1;			/* UPA2 */
185*0Sstevel@tonic-gate 	char	tr;				/* tone/transition detector */
186*0Sstevel@tonic-gate 	short	thr2;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	mag = dq & 0x3FFF;
189*0Sstevel@tonic-gate 	/* TRANS */
190*0Sstevel@tonic-gate 	if (state_ptr->td == 0) {
191*0Sstevel@tonic-gate 		tr = 0;
192*0Sstevel@tonic-gate 	} else if (state_ptr->yl > 0x40000) {
193*0Sstevel@tonic-gate 		tr = (mag <= 0x2F80) ? 0 : 1;
194*0Sstevel@tonic-gate 	} else {
195*0Sstevel@tonic-gate 		thr2 = (0x20 + ((state_ptr->yl >> 10) & 0x1F)) <<
196*0Sstevel@tonic-gate 		    (state_ptr->yl >> 15);
197*0Sstevel@tonic-gate 		if (mag >= thr2) {
198*0Sstevel@tonic-gate 			tr = 1;
199*0Sstevel@tonic-gate 		} else {
200*0Sstevel@tonic-gate 			tr = (mag <= (thr2 - (thr2 >> 2))) ? 0 : 1;
201*0Sstevel@tonic-gate 		}
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/*
205*0Sstevel@tonic-gate 	 * Quantizer scale factor adaptation.
206*0Sstevel@tonic-gate 	 */
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* FUNCTW & FILTD & DELAY */
209*0Sstevel@tonic-gate 	state_ptr->yu = y + ((_witab[i] - y) >> 5);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* LIMB */
212*0Sstevel@tonic-gate 	if (state_ptr->yu < 544) {
213*0Sstevel@tonic-gate 		state_ptr->yu = 544;
214*0Sstevel@tonic-gate 	} else if (state_ptr->yu > 5120) {
215*0Sstevel@tonic-gate 		state_ptr->yu = 5120;
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/* FILTE & DELAY */
219*0Sstevel@tonic-gate 	state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/*
222*0Sstevel@tonic-gate 	 * Adaptive predictor.
223*0Sstevel@tonic-gate 	 */
224*0Sstevel@tonic-gate 	if (tr == 1) {
225*0Sstevel@tonic-gate 		state_ptr->a[0] = 0;
226*0Sstevel@tonic-gate 		state_ptr->a[1] = 0;
227*0Sstevel@tonic-gate 		state_ptr->b[0] = 0;
228*0Sstevel@tonic-gate 		state_ptr->b[1] = 0;
229*0Sstevel@tonic-gate 		state_ptr->b[2] = 0;
230*0Sstevel@tonic-gate 		state_ptr->b[3] = 0;
231*0Sstevel@tonic-gate 		state_ptr->b[4] = 0;
232*0Sstevel@tonic-gate 		state_ptr->b[5] = 0;
233*0Sstevel@tonic-gate 	} else {
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		/* UPA2 */
236*0Sstevel@tonic-gate 		pks1 = pk0 ^ state_ptr->pk[0];
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
239*0Sstevel@tonic-gate 		if (sigpk == 0) {
240*0Sstevel@tonic-gate 			fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
241*0Sstevel@tonic-gate 			if (fa1 < -8191) {
242*0Sstevel@tonic-gate 				a2p -= 0x100;
243*0Sstevel@tonic-gate 			} else if (fa1 > 8191) {
244*0Sstevel@tonic-gate 				a2p += 0xFF;
245*0Sstevel@tonic-gate 			} else {
246*0Sstevel@tonic-gate 				a2p += fa1 >> 5;
247*0Sstevel@tonic-gate 			}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 			if (pk0 ^ state_ptr->pk[1]) {
250*0Sstevel@tonic-gate 				/* LIMC */
251*0Sstevel@tonic-gate 				if (a2p <= -12160) {
252*0Sstevel@tonic-gate 					a2p = -12288;
253*0Sstevel@tonic-gate 				} else if (a2p >= 12416) {
254*0Sstevel@tonic-gate 					a2p = 12288;
255*0Sstevel@tonic-gate 				} else {
256*0Sstevel@tonic-gate 					a2p -= 0x80;
257*0Sstevel@tonic-gate 				}
258*0Sstevel@tonic-gate 			} else if (a2p <= -12416) {
259*0Sstevel@tonic-gate 				a2p = -12288;
260*0Sstevel@tonic-gate 			} else if (a2p >= 12160) {
261*0Sstevel@tonic-gate 				a2p = 12288;
262*0Sstevel@tonic-gate 			} else {
263*0Sstevel@tonic-gate 				a2p += 0x80;
264*0Sstevel@tonic-gate 			}
265*0Sstevel@tonic-gate 		}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 		/* TRIGB & DELAY */
268*0Sstevel@tonic-gate 		state_ptr->a[1] = a2p;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 		/* UPA1 */
271*0Sstevel@tonic-gate 		state_ptr->a[0] -= state_ptr->a[0] >> 8;
272*0Sstevel@tonic-gate 		if (sigpk == 0) {
273*0Sstevel@tonic-gate 			if (pks1 == 0) {
274*0Sstevel@tonic-gate 				state_ptr->a[0] += 192;
275*0Sstevel@tonic-gate 			} else {
276*0Sstevel@tonic-gate 				state_ptr->a[0] -= 192;
277*0Sstevel@tonic-gate 			}
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 		/* LIMD */
281*0Sstevel@tonic-gate 		a1ul = 15360 - a2p;
282*0Sstevel@tonic-gate 		if (state_ptr->a[0] < -a1ul)
283*0Sstevel@tonic-gate 			state_ptr->a[0] = -a1ul;
284*0Sstevel@tonic-gate 		else if (state_ptr->a[0] > a1ul)
285*0Sstevel@tonic-gate 			state_ptr->a[0] = a1ul;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 		/* UPB : update of b's */
288*0Sstevel@tonic-gate 		for (cnt = 0; cnt < 6; cnt++) {
289*0Sstevel@tonic-gate 			state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
290*0Sstevel@tonic-gate 			if (dq & 0x3FFF) {
291*0Sstevel@tonic-gate 				/* XOR */
292*0Sstevel@tonic-gate 				if ((dq ^ state_ptr->dq[cnt]) >= 0)
293*0Sstevel@tonic-gate 					state_ptr->b[cnt] += 128;
294*0Sstevel@tonic-gate 				else
295*0Sstevel@tonic-gate 					state_ptr->b[cnt] -= 128;
296*0Sstevel@tonic-gate 			}
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	for (cnt = 5; cnt > 0; cnt--)
301*0Sstevel@tonic-gate 		state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
302*0Sstevel@tonic-gate 	/* FLOAT A */
303*0Sstevel@tonic-gate 	if (mag == 0) {
304*0Sstevel@tonic-gate 		state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
305*0Sstevel@tonic-gate 	} else {
306*0Sstevel@tonic-gate 		exp = _fmultanexp[mag];
307*0Sstevel@tonic-gate 		state_ptr->dq[0] = (dq >= 0) ?
308*0Sstevel@tonic-gate 		    (exp << 6) + ((mag << 6) >> exp) :
309*0Sstevel@tonic-gate 		    (exp << 6) + ((mag << 6) >> exp) - 0x400;
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	state_ptr->sr[1] = state_ptr->sr[0];
313*0Sstevel@tonic-gate 	/* FLOAT B */
314*0Sstevel@tonic-gate 	if (sr == 0) {
315*0Sstevel@tonic-gate 		state_ptr->sr[0] = 0x20;
316*0Sstevel@tonic-gate 	} else if (sr > 0) {
317*0Sstevel@tonic-gate 		exp = _fmultanexp[sr];
318*0Sstevel@tonic-gate 		state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
319*0Sstevel@tonic-gate 	} else {
320*0Sstevel@tonic-gate 		mag = -sr;
321*0Sstevel@tonic-gate 		exp = _fmultanexp[mag];
322*0Sstevel@tonic-gate 		state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	/* DELAY A */
326*0Sstevel@tonic-gate 	state_ptr->pk[1] = state_ptr->pk[0];
327*0Sstevel@tonic-gate 	state_ptr->pk[0] = pk0;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	/* TONE */
330*0Sstevel@tonic-gate 	if (tr == 1)
331*0Sstevel@tonic-gate 		state_ptr->td = 0;
332*0Sstevel@tonic-gate 	else if (a2p < -11776)
333*0Sstevel@tonic-gate 		state_ptr->td = 1;
334*0Sstevel@tonic-gate 	else
335*0Sstevel@tonic-gate 		state_ptr->td = 0;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/*
338*0Sstevel@tonic-gate 	 * Adaptation speed control.
339*0Sstevel@tonic-gate 	 */
340*0Sstevel@tonic-gate 	fi = _fitab[i];						/* FUNCTF */
341*0Sstevel@tonic-gate 	state_ptr->dms += (fi - state_ptr->dms) >> 5;		/* FILTA */
342*0Sstevel@tonic-gate 	state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);	/* FILTB */
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	if (tr == 1)
345*0Sstevel@tonic-gate 		state_ptr->ap = 256;
346*0Sstevel@tonic-gate 	else if (y < 1536)					/* SUBTC */
347*0Sstevel@tonic-gate 		state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
348*0Sstevel@tonic-gate 	else if (state_ptr->td == 1)
349*0Sstevel@tonic-gate 		state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
350*0Sstevel@tonic-gate 	else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
351*0Sstevel@tonic-gate 	    (state_ptr->dml >> 3))
352*0Sstevel@tonic-gate 		state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
353*0Sstevel@tonic-gate 	else
354*0Sstevel@tonic-gate 		state_ptr->ap += (-state_ptr->ap) >> 4;
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate /*
358*0Sstevel@tonic-gate  * _g721_quantize()
359*0Sstevel@tonic-gate  *
360*0Sstevel@tonic-gate  * Description:
361*0Sstevel@tonic-gate  *
362*0Sstevel@tonic-gate  * Given a raw sample, 'd', of the difference signal and a
363*0Sstevel@tonic-gate  * quantization step size scale factor, 'y', this routine returns the
364*0Sstevel@tonic-gate  * G.721 codeword to which that sample gets quantized.  The step
365*0Sstevel@tonic-gate  * size scale factor division operation is done in the log base 2 domain
366*0Sstevel@tonic-gate  * as a subtraction.
367*0Sstevel@tonic-gate  */
368*0Sstevel@tonic-gate static unsigned int
_g721_quantize(int d,int y)369*0Sstevel@tonic-gate _g721_quantize(
370*0Sstevel@tonic-gate 	int	d,	/* Raw difference signal sample. */
371*0Sstevel@tonic-gate 	int	y)	/* Step size multiplier. */
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	/* LOG */
374*0Sstevel@tonic-gate 	short	dqm;	/* Magnitude of 'd'. */
375*0Sstevel@tonic-gate 	short	exp;	/* Integer part of base 2 log of magnitude of 'd'. */
376*0Sstevel@tonic-gate 	short	mant;	/* Fractional part of base 2 log. */
377*0Sstevel@tonic-gate 	short	dl;	/* Log of magnitude of 'd'. */
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	/* SUBTB */
380*0Sstevel@tonic-gate 	short	dln;	/* Step size scale factor normalized log. */
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	/* QUAN */
383*0Sstevel@tonic-gate 	char	i;	/* G.721 codeword. */
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	/*
386*0Sstevel@tonic-gate 	 * LOG
387*0Sstevel@tonic-gate 	 *
388*0Sstevel@tonic-gate 	 * Compute base 2 log of 'd', and store in 'dln'.
389*0Sstevel@tonic-gate 	 *
390*0Sstevel@tonic-gate 	 */
391*0Sstevel@tonic-gate 	dqm = abs(d);
392*0Sstevel@tonic-gate 	exp = _fmultanexp[dqm >> 1];
393*0Sstevel@tonic-gate 	mant = ((dqm << 7) >> exp) & 0x7F;	/* Fractional portion. */
394*0Sstevel@tonic-gate 	dl = (exp << 7) + mant;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/*
397*0Sstevel@tonic-gate 	 * SUBTB
398*0Sstevel@tonic-gate 	 *
399*0Sstevel@tonic-gate 	 * "Divide" by step size multiplier.
400*0Sstevel@tonic-gate 	 */
401*0Sstevel@tonic-gate 	dln = dl - (y >> 2);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	/*
404*0Sstevel@tonic-gate 	 * QUAN
405*0Sstevel@tonic-gate 	 *
406*0Sstevel@tonic-gate 	 * Obtain codword for 'd'.
407*0Sstevel@tonic-gate 	 */
408*0Sstevel@tonic-gate 	i = _quani[dln & 0xFFF];
409*0Sstevel@tonic-gate 	if (d < 0)
410*0Sstevel@tonic-gate 		i ^= 0xF;	/* Stuff in sign of 'd'. */
411*0Sstevel@tonic-gate 	else if (i == 0)
412*0Sstevel@tonic-gate 		i = 0xF;	/* New in 1988 revision */
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	return (i);
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * _g721_reconstr()
419*0Sstevel@tonic-gate  *
420*0Sstevel@tonic-gate  * Description:
421*0Sstevel@tonic-gate  *
422*0Sstevel@tonic-gate  * Returns reconstructed difference signal 'dq' obtained from
423*0Sstevel@tonic-gate  * G.721 codeword 'i' and quantization step size scale factor 'y'.
424*0Sstevel@tonic-gate  * Multiplication is performed in log base 2 domain as addition.
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate static unsigned long
_g721_reconstr(int i,unsigned long y)427*0Sstevel@tonic-gate _g721_reconstr(
428*0Sstevel@tonic-gate 	int		i,	/* G.721 codeword. */
429*0Sstevel@tonic-gate 	unsigned long	y)	/* Step size multiplier. */
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	/* ADD A */
432*0Sstevel@tonic-gate 	short	dql;	/* Log of 'dq' magnitude. */
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	/* ANTILOG */
435*0Sstevel@tonic-gate 	short	dex;	/* Integer part of log. */
436*0Sstevel@tonic-gate 	short	dqt;
437*0Sstevel@tonic-gate 	short	dq;	/* Reconstructed difference signal sample. */
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	dql = _dqlntab[i] + (y >> 2);	/* ADDA */
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	if (dql < 0)
442*0Sstevel@tonic-gate 		dq = 0;
443*0Sstevel@tonic-gate 	else {				/* ANTILOG */
444*0Sstevel@tonic-gate 		dex = (dql >> 7) & 15;
445*0Sstevel@tonic-gate 		dqt = 128 + (dql & 127);
446*0Sstevel@tonic-gate 		dq = (dqt << 7) >> (14 - dex);
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	if (i & 8)
449*0Sstevel@tonic-gate 		dq -= 0x4000;
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	return (dq);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate /*
455*0Sstevel@tonic-gate  * _tandem_adjust(sr, se, y, i)
456*0Sstevel@tonic-gate  *
457*0Sstevel@tonic-gate  * Description:
458*0Sstevel@tonic-gate  *
459*0Sstevel@tonic-gate  * At the end of ADPCM decoding, it simulates an encoder which may be receiving
460*0Sstevel@tonic-gate  * the output of this decoder as a tandem process. If the output of the
461*0Sstevel@tonic-gate  * simulated encoder differs from the input to this decoder, the decoder output
462*0Sstevel@tonic-gate  * is adjusted by one level of A-law or u-law codes.
463*0Sstevel@tonic-gate  *
464*0Sstevel@tonic-gate  * Input:
465*0Sstevel@tonic-gate  *	sr	decoder output linear PCM sample,
466*0Sstevel@tonic-gate  *	se	predictor estimate sample,
467*0Sstevel@tonic-gate  *	y	quantizer step size,
468*0Sstevel@tonic-gate  *	i	decoder input code
469*0Sstevel@tonic-gate  *
470*0Sstevel@tonic-gate  * Return:
471*0Sstevel@tonic-gate  *	adjusted A-law or u-law compressed sample.
472*0Sstevel@tonic-gate  */
473*0Sstevel@tonic-gate static int
_tandem_adjust_alaw(int sr,int se,int y,int i)474*0Sstevel@tonic-gate _tandem_adjust_alaw(
475*0Sstevel@tonic-gate 	int	sr,	/* decoder output linear PCM sample */
476*0Sstevel@tonic-gate 	int	se,	/* predictor estimate sample */
477*0Sstevel@tonic-gate 	int	y,	/* quantizer step size */
478*0Sstevel@tonic-gate 	int	i)	/* decoder input code */
479*0Sstevel@tonic-gate {
480*0Sstevel@tonic-gate 	unsigned char	sp;	/* A-law compressed 8-bit code */
481*0Sstevel@tonic-gate 	short	dx;		/* prediction error */
482*0Sstevel@tonic-gate 	char	id;		/* quantized prediction error */
483*0Sstevel@tonic-gate 	int	sd;		/* adjusted A-law decoded sample value */
484*0Sstevel@tonic-gate 	int	im;		/* biased magnitude of i */
485*0Sstevel@tonic-gate 	int	imx;		/* biased magnitude of id */
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	sp = audio_s2a((sr <= -0x2000)? -0x8000 :
488*0Sstevel@tonic-gate 	    (sr >= 0x1FFF)? 0x7FFF : sr << 2);	/* short to A-law compression */
489*0Sstevel@tonic-gate 	dx = (audio_a2s(sp) >> 2) - se; 	/* 16-bit prediction error */
490*0Sstevel@tonic-gate 	id = _g721_quantize(dx, y);
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 	if (id == i)			/* no adjustment on sp */
493*0Sstevel@tonic-gate 		return (sp);
494*0Sstevel@tonic-gate 	else {				/* sp adjustment needed */
495*0Sstevel@tonic-gate 		/* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
496*0Sstevel@tonic-gate 		im = i ^ 8;		/* 2's complement to biased unsigned */
497*0Sstevel@tonic-gate 		imx = id ^ 8;
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		if (imx > im) {		/* sp adjusted to next lower value */
500*0Sstevel@tonic-gate 			if (sp & 0x80)
501*0Sstevel@tonic-gate 				sd = (sp == 0xD5)? 0x55 :
502*0Sstevel@tonic-gate 				    ((sp ^ 0x55) - 1) ^ 0x55;
503*0Sstevel@tonic-gate 			else
504*0Sstevel@tonic-gate 				sd = (sp == 0x2A)? 0x2A :
505*0Sstevel@tonic-gate 				    ((sp ^ 0x55) + 1) ^ 0x55;
506*0Sstevel@tonic-gate 		} else {		/* sp adjusted to next higher value */
507*0Sstevel@tonic-gate 			if (sp & 0x80)
508*0Sstevel@tonic-gate 				sd = (sp == 0xAA)? 0xAA :
509*0Sstevel@tonic-gate 				    ((sp ^ 0x55) + 1) ^ 0x55;
510*0Sstevel@tonic-gate 			else
511*0Sstevel@tonic-gate 				sd = (sp == 0x55)? 0xD5 :
512*0Sstevel@tonic-gate 				    ((sp ^ 0x55) - 1) ^ 0x55;
513*0Sstevel@tonic-gate 		}
514*0Sstevel@tonic-gate 		return (sd);
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate }
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate static int
_tandem_adjust_ulaw(int sr,int se,int y,int i)519*0Sstevel@tonic-gate _tandem_adjust_ulaw(
520*0Sstevel@tonic-gate 	int	sr,	/* decoder output linear PCM sample */
521*0Sstevel@tonic-gate 	int	se,	/* predictor estimate sample */
522*0Sstevel@tonic-gate 	int	y,	/* quantizer step size */
523*0Sstevel@tonic-gate 	int	i)	/* decoder input code */
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	unsigned char   sp;	/* A-law compressed 8-bit code */
526*0Sstevel@tonic-gate 	short	dx;		/* prediction error */
527*0Sstevel@tonic-gate 	char	id;		/* quantized prediction error */
528*0Sstevel@tonic-gate 	int	sd;		/* adjusted A-law decoded sample value */
529*0Sstevel@tonic-gate 	int	im;		/* biased magnitude of i */
530*0Sstevel@tonic-gate 	int	imx;		/* biased magnitude of id */
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	sp = audio_s2u((sr <= -0x2000)? -0x8000 :
533*0Sstevel@tonic-gate 	    (sr >= 0x1FFF)? 0x7FFF : sr << 2); /* short to u-law compression */
534*0Sstevel@tonic-gate 	dx = (audio_u2s(sp) >> 2) - se;  /* 16-bit prediction error */
535*0Sstevel@tonic-gate 	id = _g721_quantize(dx, y);
536*0Sstevel@tonic-gate 	if (id == i)
537*0Sstevel@tonic-gate 		return (sp);
538*0Sstevel@tonic-gate 	else {
539*0Sstevel@tonic-gate 		/* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
540*0Sstevel@tonic-gate 		im = i ^ 8;		/* 2's complement to biased unsigned */
541*0Sstevel@tonic-gate 		imx = id ^ 8;
542*0Sstevel@tonic-gate 		if (imx > im) {		/* sp adjusted to next lower value */
543*0Sstevel@tonic-gate 			if (sp & 0x80)
544*0Sstevel@tonic-gate 				sd = (sp == 0xFF)? 0x7F : sp + 1;
545*0Sstevel@tonic-gate 			else
546*0Sstevel@tonic-gate 				sd = (sp == 0)? 0 : sp - 1;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 		} else {		/* sp adjusted to next higher value */
549*0Sstevel@tonic-gate 			if (sp & 0x80)
550*0Sstevel@tonic-gate 				sd = (sp == 0x80)? 0x80 : sp - 1;
551*0Sstevel@tonic-gate 			else
552*0Sstevel@tonic-gate 				sd = (sp == 0x7F)? 0xFF : sp + 1;
553*0Sstevel@tonic-gate 		}
554*0Sstevel@tonic-gate 		return (sd);
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate }
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate /*
559*0Sstevel@tonic-gate  * g721_encode()
560*0Sstevel@tonic-gate  *
561*0Sstevel@tonic-gate  * Description:
562*0Sstevel@tonic-gate  *
563*0Sstevel@tonic-gate  * Encodes a buffer of linear PCM, A-law or u-law data pointed to by
564*0Sstevel@tonic-gate  * 'in_buf' according * the G.721 encoding algorithm and packs the
565*0Sstevel@tonic-gate  * resulting code words into bytes. The bytes of codeword pairs are
566*0Sstevel@tonic-gate  * written to a buffer pointed to by 'out_buf'.
567*0Sstevel@tonic-gate  *
568*0Sstevel@tonic-gate  * Notes:
569*0Sstevel@tonic-gate  *
570*0Sstevel@tonic-gate  * In the event that the total number of codewords which have to be
571*0Sstevel@tonic-gate  * written is odd, the last unpairable codeword is saved in the
572*0Sstevel@tonic-gate  * state structure till the next call. It is then paired off and
573*0Sstevel@tonic-gate  * packed with the first codeword of the new buffer. The number of
574*0Sstevel@tonic-gate  * valid bytes in 'out_buf' is returned in *out_size. Note that
575*0Sstevel@tonic-gate  * *out_size will not always be equal to half * of 'data_size' on input.
576*0Sstevel@tonic-gate  * On the final call to 'g721_encode()' the calling program might want to
577*0Sstevel@tonic-gate  * check if a codeword was left over. This can be
578*0Sstevel@tonic-gate  * done by calling 'g721_encode()' with data_size = 0, which returns in
579*0Sstevel@tonic-gate  * *out_size a 0 if nothing was leftover and 1 if a codeword was leftover
580*0Sstevel@tonic-gate  * which now is in out_buf[0].
581*0Sstevel@tonic-gate  *
582*0Sstevel@tonic-gate  * The 4 lower significant bits of an individual byte in the output byte
583*0Sstevel@tonic-gate  * stream is packed with a G.721 codeword first.  Then the 4 higher order
584*0Sstevel@tonic-gate  * bits are packed with the next codeword.
585*0Sstevel@tonic-gate  */
586*0Sstevel@tonic-gate int
g721_encode(void * in_buf,int data_size,Audio_hdr * in_header,unsigned char * out_buf,int * out_size,struct audio_g72x_state * state_ptr)587*0Sstevel@tonic-gate g721_encode(
588*0Sstevel@tonic-gate 	void		*in_buf,
589*0Sstevel@tonic-gate 	int		data_size,
590*0Sstevel@tonic-gate 	Audio_hdr	*in_header,
591*0Sstevel@tonic-gate 	unsigned char	*out_buf,
592*0Sstevel@tonic-gate 	int		*out_size,
593*0Sstevel@tonic-gate 	struct audio_g72x_state *state_ptr)
594*0Sstevel@tonic-gate {
595*0Sstevel@tonic-gate 	short	sl;				/* EXPAND */
596*0Sstevel@tonic-gate 	short	sei, sezi, se, sez;		/* ACCUM */
597*0Sstevel@tonic-gate 	short	d;				/* SUBTA */
598*0Sstevel@tonic-gate 	float	al;		/* use floating point for faster multiply */
599*0Sstevel@tonic-gate 	short	y, dif;				/* MIX */
600*0Sstevel@tonic-gate 	short	sr;				/* ADDB */
601*0Sstevel@tonic-gate 	short	pk0, sigpk, dqsez;		/* ADDC */
602*0Sstevel@tonic-gate 	short	dq, i;
603*0Sstevel@tonic-gate 	int	cnt, cnta;
604*0Sstevel@tonic-gate 	int	out_leng;
605*0Sstevel@tonic-gate 	unsigned char *char_in;
606*0Sstevel@tonic-gate 	unsigned char *char_out;
607*0Sstevel@tonic-gate 	short	*short_ptr;
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	if (data_size == 0) {
610*0Sstevel@tonic-gate 		/* Actually, the leftover count will never be more than 4 */
611*0Sstevel@tonic-gate 		for (i = 0; state_ptr->leftover_cnt > 0; i++) {
612*0Sstevel@tonic-gate 			*out_buf++ = state_ptr->leftover[i];
613*0Sstevel@tonic-gate 			state_ptr->leftover_cnt -= 8;
614*0Sstevel@tonic-gate 		}
615*0Sstevel@tonic-gate 		*out_size = i;
616*0Sstevel@tonic-gate 		state_ptr->leftover_cnt = 0;
617*0Sstevel@tonic-gate 		return (AUDIO_SUCCESS);
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	/* XXX - if linear, it had better be 16-bit! */
621*0Sstevel@tonic-gate 	if (in_header->encoding == AUDIO_ENCODING_LINEAR) {
622*0Sstevel@tonic-gate 		if (data_size & 1) {
623*0Sstevel@tonic-gate 			return (AUDIO_ERR_BADFRAME);
624*0Sstevel@tonic-gate 		} else {
625*0Sstevel@tonic-gate 			data_size >>= 1;	/* divide to get sample cnt */
626*0Sstevel@tonic-gate 			short_ptr = (short *)in_buf;
627*0Sstevel@tonic-gate 		}
628*0Sstevel@tonic-gate 	} else {
629*0Sstevel@tonic-gate 		char_in = (unsigned char *)in_buf;
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 	char_out = (unsigned char *)out_buf;
632*0Sstevel@tonic-gate 	if (state_ptr->leftover_cnt > 0) {
633*0Sstevel@tonic-gate 		*char_out = state_ptr->leftover[0];
634*0Sstevel@tonic-gate 		state_ptr->leftover_cnt = 0;
635*0Sstevel@tonic-gate 		data_size += 1;
636*0Sstevel@tonic-gate 		cnta = 1;
637*0Sstevel@tonic-gate 	} else {
638*0Sstevel@tonic-gate 		cnta = 0;
639*0Sstevel@tonic-gate 	}
640*0Sstevel@tonic-gate 	out_leng = (data_size & ~0x01);		/* clear low order bit */
641*0Sstevel@tonic-gate 	for (; cnta < data_size; cnta++) {
642*0Sstevel@tonic-gate 		/*  EXPAND  */
643*0Sstevel@tonic-gate 		switch (in_header->encoding) {
644*0Sstevel@tonic-gate 		case AUDIO_ENCODING_LINEAR:
645*0Sstevel@tonic-gate 			sl = *short_ptr++ >> 2;
646*0Sstevel@tonic-gate 			break;
647*0Sstevel@tonic-gate 		case AUDIO_ENCODING_ALAW:
648*0Sstevel@tonic-gate 			sl = audio_a2s(*char_in++) >> 2;
649*0Sstevel@tonic-gate 			break;
650*0Sstevel@tonic-gate 		case AUDIO_ENCODING_ULAW:
651*0Sstevel@tonic-gate 			sl = audio_u2s(*char_in++) >> 2; /* u-law to short */
652*0Sstevel@tonic-gate 			break;
653*0Sstevel@tonic-gate 		default:
654*0Sstevel@tonic-gate 			return (AUDIO_ERR_ENCODING);
655*0Sstevel@tonic-gate 		}
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 		/* ACCUM */
658*0Sstevel@tonic-gate 		sezi = _g721_fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
659*0Sstevel@tonic-gate 		for (cnt = 1; cnt < 6; cnt++)
660*0Sstevel@tonic-gate 			sezi = sezi + _g721_fmult(state_ptr->b[cnt] >> 2,
661*0Sstevel@tonic-gate 			    state_ptr->dq[cnt]);
662*0Sstevel@tonic-gate 		sei = sezi;
663*0Sstevel@tonic-gate 		for (cnt = 1; cnt > -1; cnt--)
664*0Sstevel@tonic-gate 			sei = sei + _g721_fmult(state_ptr->a[cnt] >> 2,
665*0Sstevel@tonic-gate 			    state_ptr->sr[cnt]);
666*0Sstevel@tonic-gate 		sez = sezi >> 1;
667*0Sstevel@tonic-gate 		se = sei >> 1;
668*0Sstevel@tonic-gate 		d = sl - se;				/* SUBTA */
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 		if (state_ptr->ap >= 256)
671*0Sstevel@tonic-gate 			y = state_ptr->yu;
672*0Sstevel@tonic-gate 		else {
673*0Sstevel@tonic-gate 			y = state_ptr->yl >> 6;
674*0Sstevel@tonic-gate 			dif = state_ptr->yu - y;
675*0Sstevel@tonic-gate 			al = state_ptr->ap >> 2;
676*0Sstevel@tonic-gate 			if (dif > 0)
677*0Sstevel@tonic-gate 				y += ((int)(dif * al)) >> 6;
678*0Sstevel@tonic-gate 			else if (dif < 0)
679*0Sstevel@tonic-gate 				y += ((int)(dif * al) + 0x3F) >> 6;
680*0Sstevel@tonic-gate 		}
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 		i = _g721_quantize(d, y);
683*0Sstevel@tonic-gate 		dq = _g721_reconstr(i, y);
684*0Sstevel@tonic-gate 		/* ADDB */
685*0Sstevel@tonic-gate 		sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 		if (cnta & 1) {
688*0Sstevel@tonic-gate 			*char_out++ += i << 4;
689*0Sstevel@tonic-gate 		} else if (cnta < out_leng) {
690*0Sstevel@tonic-gate 			*char_out = i;
691*0Sstevel@tonic-gate 		} else {
692*0Sstevel@tonic-gate 			/*
693*0Sstevel@tonic-gate 			 * save the last codeword which can not be paired into
694*0Sstevel@tonic-gate 			 * a byte in the state stucture and set leftover_flag.
695*0Sstevel@tonic-gate 			 */
696*0Sstevel@tonic-gate 			state_ptr->leftover[0] = i;
697*0Sstevel@tonic-gate 			state_ptr->leftover_cnt = 4;
698*0Sstevel@tonic-gate 		}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 		dqsez = sr + sez - se;		/* ADDC */
701*0Sstevel@tonic-gate 		if (dqsez == 0) {
702*0Sstevel@tonic-gate 			pk0 = 0;
703*0Sstevel@tonic-gate 			sigpk = 1;
704*0Sstevel@tonic-gate 		} else {
705*0Sstevel@tonic-gate 			pk0 = (dqsez < 0) ? 1 : 0;
706*0Sstevel@tonic-gate 			sigpk = 0;
707*0Sstevel@tonic-gate 		}
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		_g721_update(y, i, dq, sr, pk0, state_ptr, sigpk);
710*0Sstevel@tonic-gate 	}
711*0Sstevel@tonic-gate 	*out_size = cnta >> 1;
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
714*0Sstevel@tonic-gate }
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate /*
717*0Sstevel@tonic-gate  * g721_decode()
718*0Sstevel@tonic-gate  *
719*0Sstevel@tonic-gate  * Description:
720*0Sstevel@tonic-gate  *
721*0Sstevel@tonic-gate  * Decodes a buffer of G.721 encoded data pointed to by 'in_buf' and
722*0Sstevel@tonic-gate  * writes the resulting linear PCM, A-law or Mu-law bytes into a buffer
723*0Sstevel@tonic-gate  * pointed to by 'out_buf'.
724*0Sstevel@tonic-gate  */
725*0Sstevel@tonic-gate int
g721_decode(unsigned char * in_buf,int data_size,Audio_hdr * out_header,void * out_buf,int * out_size,struct audio_g72x_state * state_ptr)726*0Sstevel@tonic-gate g721_decode(
727*0Sstevel@tonic-gate 	unsigned char	*in_buf,	/* Buffer of g721 encoded data. */
728*0Sstevel@tonic-gate 	int		data_size,	/* Size in bytes of in_buf. */
729*0Sstevel@tonic-gate 	Audio_hdr	*out_header,
730*0Sstevel@tonic-gate 	void		*out_buf,	/* Decoded data buffer. */
731*0Sstevel@tonic-gate 	int		*out_size,
732*0Sstevel@tonic-gate 	struct audio_g72x_state *state_ptr) /* the decoder's state structure. */
733*0Sstevel@tonic-gate {
734*0Sstevel@tonic-gate 	short	sezi, sei, sez, se;		/* ACCUM */
735*0Sstevel@tonic-gate 	float	al;		/* use floating point for faster multiply */
736*0Sstevel@tonic-gate 	short	y, dif;				/* MIX */
737*0Sstevel@tonic-gate 	short sr;				/* ADDB */
738*0Sstevel@tonic-gate 	char	pk0, i;				/* ADDC */
739*0Sstevel@tonic-gate 	short	dq;
740*0Sstevel@tonic-gate 	char	sigpk;
741*0Sstevel@tonic-gate 	short	dqsez;
742*0Sstevel@tonic-gate 	unsigned char *char_in;
743*0Sstevel@tonic-gate 	unsigned char *char_out;
744*0Sstevel@tonic-gate 	int	cnt, cnta;
745*0Sstevel@tonic-gate 	short	*linear_out;
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	*out_size = data_size << 1;
748*0Sstevel@tonic-gate 	char_in = (unsigned char *)in_buf;
749*0Sstevel@tonic-gate 	char_out = (unsigned char *)out_buf;
750*0Sstevel@tonic-gate 	linear_out = (short *)out_buf;
751*0Sstevel@tonic-gate 	for (cnta = 0; cnta < *out_size; cnta++) {
752*0Sstevel@tonic-gate 		if (cnta & 1)
753*0Sstevel@tonic-gate 			i = *char_in++ >> 4;
754*0Sstevel@tonic-gate 		else
755*0Sstevel@tonic-gate 			i = *char_in & 0xF;
756*0Sstevel@tonic-gate 		/* ACCUM */
757*0Sstevel@tonic-gate 		sezi = _g721_fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
758*0Sstevel@tonic-gate 		for (cnt = 1; cnt < 6; cnt++)
759*0Sstevel@tonic-gate 			sezi = sezi + _g721_fmult(state_ptr->b[cnt] >> 2,
760*0Sstevel@tonic-gate 			    state_ptr->dq[cnt]);
761*0Sstevel@tonic-gate 		sei = sezi;
762*0Sstevel@tonic-gate 		for (cnt = 1; cnt >= 0; cnt--)
763*0Sstevel@tonic-gate 			sei = sei + _g721_fmult(state_ptr->a[cnt] >> 2,
764*0Sstevel@tonic-gate 			    state_ptr->sr[cnt]);
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		sez = sezi >> 1;
767*0Sstevel@tonic-gate 		se = sei >> 1;
768*0Sstevel@tonic-gate 		if (state_ptr->ap >= 256)
769*0Sstevel@tonic-gate 			y = state_ptr->yu;
770*0Sstevel@tonic-gate 		else {
771*0Sstevel@tonic-gate 			y = state_ptr->yl >> 6;
772*0Sstevel@tonic-gate 			dif = state_ptr->yu - y;
773*0Sstevel@tonic-gate 			al = state_ptr->ap >> 2;
774*0Sstevel@tonic-gate 			if (dif > 0)
775*0Sstevel@tonic-gate 				y += ((int)(dif * al)) >> 6;
776*0Sstevel@tonic-gate 			else if (dif < 0)
777*0Sstevel@tonic-gate 				y += ((int)(dif * al) + 0x3F) >> 6;
778*0Sstevel@tonic-gate 		}
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate 		dq = _g721_reconstr(i, y);
781*0Sstevel@tonic-gate 		/* ADDB */
782*0Sstevel@tonic-gate 		if (dq < 0)
783*0Sstevel@tonic-gate 			sr = se - (dq & 0x3FFF);
784*0Sstevel@tonic-gate 		else
785*0Sstevel@tonic-gate 			sr = se + dq;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 		switch (out_header->encoding) {
788*0Sstevel@tonic-gate 		case AUDIO_ENCODING_LINEAR:
789*0Sstevel@tonic-gate 			*linear_out++ = ((sr <= -0x2000) ? -0x8000 :
790*0Sstevel@tonic-gate 			    (sr >= 0x1FFF) ? 0x7FFF : sr << 2);
791*0Sstevel@tonic-gate 			break;
792*0Sstevel@tonic-gate 		case AUDIO_ENCODING_ALAW:
793*0Sstevel@tonic-gate 			*char_out++ = _tandem_adjust_alaw(sr, se, y, i);
794*0Sstevel@tonic-gate 			break;
795*0Sstevel@tonic-gate 		case AUDIO_ENCODING_ULAW:
796*0Sstevel@tonic-gate 			*char_out++ = _tandem_adjust_ulaw(sr, se, y, i);
797*0Sstevel@tonic-gate 			break;
798*0Sstevel@tonic-gate 		default:
799*0Sstevel@tonic-gate 			return (AUDIO_ERR_ENCODING);
800*0Sstevel@tonic-gate 		}
801*0Sstevel@tonic-gate 		/* ADDC */
802*0Sstevel@tonic-gate 		dqsez = sr - se + sez;
803*0Sstevel@tonic-gate 		pk0 = (dqsez < 0) ? 1 : 0;
804*0Sstevel@tonic-gate 		sigpk = (dqsez) ? 0 : 1;
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 		_g721_update(y, i, dq, sr, pk0, state_ptr, sigpk);
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 	*out_size = cnta;
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
811*0Sstevel@tonic-gate }
812