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