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