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