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