home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / g723_24.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  5KB  |  161 lines

  1. /*
  2.  * This source code is a product of Sun Microsystems, Inc. and is provided
  3.  * for unrestricted use.  Users may copy or modify this source code without
  4.  * charge.
  5.  *
  6.  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
  7.  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  8.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  9.  *
  10.  * Sun source code is provided with no support and without any obligation on
  11.  * the part of Sun Microsystems, Inc. to assist in its use, correction,
  12.  * modification or enhancement.
  13.  *
  14.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  15.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
  16.  * OR ANY PART THEREOF.
  17.  *
  18.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  19.  * or profits or other special, indirect and consequential damages, even if
  20.  * Sun has been advised of the possibility of such damages.
  21.  *
  22.  * Sun Microsystems, Inc.
  23.  * 2550 Garcia Avenue
  24.  * Mountain View, California  94043
  25.  */
  26.  
  27. /*
  28.  * g723_24.c
  29.  *
  30.  * Description:
  31.  *
  32.  * g723_24_encoder(), g723_24_decoder()
  33.  *
  34.  * These routines comprise an implementation of the CCITT G.723 24 Kbps
  35.  * ADPCM coding algorithm.  Essentially, this implementation is identical to
  36.  * the bit level description except for a few deviations which take advantage
  37.  * of workstation attributes, such as hardware 2's complement arithmetic.
  38.  *
  39.  */
  40. #include "st.h"
  41. #include "libst.h"
  42. #include "g72x.h"
  43.  
  44. /*
  45.  * Maps G.723_24 code word to reconstructed scale factor normalized log
  46.  * magnitude values.
  47.  */
  48. static short    _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
  49.  
  50. /* Maps G.723_24 code word to log of scale factor multiplier. */
  51. static short    _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
  52.  
  53. /*
  54.  * Maps G.723_24 code words to a set of values whose long and short
  55.  * term averages are computed and then compared to give an indication
  56.  * how stationary (steady state) the signal is.
  57.  */
  58. static short    _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
  59.  
  60. static short qtab_723_24[3] = {8, 218, 331};
  61.  
  62. /*
  63.  * g723_24_encoder()
  64.  *
  65.  * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
  66.  * Returns -1 if invalid input coding value.
  67.  */
  68. int
  69. g723_24_encoder(sl, in_coding, state_ptr)
  70.     int        sl;
  71.     int        in_coding;
  72.     struct g72x_state *state_ptr;
  73. {
  74.     short        sei, sezi, se, sez;    /* ACCUM */
  75.     short        d;            /* SUBTA */
  76.     short        y;            /* MIX */
  77.     short        sr;            /* ADDB */
  78.     short        dqsez;            /* ADDC */
  79.     short        dq, i;
  80.  
  81.     switch (in_coding) {    /* linearize input sample to 14-bit PCM */
  82.     case AUDIO_ENCODING_ALAW:
  83.         sl = st_Alaw_to_linear(sl) >> 2;
  84.         break;
  85.     case AUDIO_ENCODING_ULAW:
  86.         sl = st_ulaw_to_linear(sl) >> 2;
  87.         break;
  88.     case AUDIO_ENCODING_LINEAR:
  89.         sl >>= 2;        /* sl of 14-bit dynamic range */
  90.         break;
  91.     default:
  92.         return (-1);
  93.     }
  94.  
  95.     sezi = predictor_zero(state_ptr);
  96.     sez = sezi >> 1;
  97.     sei = sezi + predictor_pole(state_ptr);
  98.     se = sei >> 1;            /* se = estimated signal */
  99.  
  100.     d = sl - se;            /* d = estimation diff. */
  101.  
  102.     /* quantize prediction difference d */
  103.     y = step_size(state_ptr);    /* quantizer step size */
  104.     i = quantize(d, y, qtab_723_24, 3);    /* i = ADPCM code */
  105.     dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
  106.  
  107.     sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
  108.  
  109.     dqsez = sr + sez - se;        /* pole prediction diff. */
  110.  
  111.     update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
  112.  
  113.     return (i);
  114. }
  115.  
  116. /*
  117.  * g723_24_decoder()
  118.  *
  119.  * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
  120.  * the resulting 16-bit linear PCM, A-law or u-law sample value.
  121.  * -1 is returned if the output coding is unknown.
  122.  */
  123. int
  124. g723_24_decoder(i, out_coding, state_ptr)
  125.     int        i;
  126.     int        out_coding;
  127.     struct g72x_state *state_ptr;
  128. {
  129.     short        sezi, sei, sez, se;    /* ACCUM */
  130.     short        y;            /* MIX */
  131.     short        sr;            /* ADDB */
  132.     short        dq;
  133.     short        dqsez;
  134.  
  135.     i &= 0x07;            /* mask to get proper bits */
  136.     sezi = predictor_zero(state_ptr);
  137.     sez = sezi >> 1;
  138.     sei = sezi + predictor_pole(state_ptr);
  139.     se = sei >> 1;            /* se = estimated signal */
  140.  
  141.     y = step_size(state_ptr);    /* adaptive quantizer step size */
  142.     dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
  143.  
  144.     sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
  145.  
  146.     dqsez = sr - se + sez;            /* pole prediction diff. */
  147.  
  148.     update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
  149.  
  150.     switch (out_coding) {
  151.     case AUDIO_ENCODING_ALAW:
  152.         return (tandem_adjust_alaw(sr, se, y, i, 4, qtab_723_24));
  153.     case AUDIO_ENCODING_ULAW:
  154.         return (tandem_adjust_ulaw(sr, se, y, i, 4, qtab_723_24));
  155.     case AUDIO_ENCODING_LINEAR:
  156.         return (sr << 2);    /* sr was of 14-bit dynamic range */
  157.     default:
  158.         return (-1);
  159.     }
  160. }
  161.