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