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