home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / g72x.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  15KB  |  568 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.  * g72x.c
  29.  *
  30.  * Common routines for G.721 and G.723 conversions.
  31.  */
  32.  
  33. #include "st.h"
  34. #include "libst.h"
  35. #include "g72x.h"
  36.  
  37. static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
  38.             0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
  39.  
  40. /*
  41.  * quan()
  42.  *
  43.  * quantizes the input val against the table of size short integers.
  44.  * It returns i if table[i - 1] <= val < table[i].
  45.  *
  46.  * Using linear search for simple coding.
  47.  */
  48. static int
  49. quan(val,table,size)
  50.     int        val;
  51.     short        *table;
  52.     int        size;
  53. {
  54.     int        i;
  55.  
  56.     for (i = 0; i < size; i++)
  57.         if (val < *table++)
  58.             break;
  59.     return (i);
  60. }
  61.  
  62. /*
  63.  * fmult()
  64.  *
  65.  * returns the integer product of the 14-bit integer "an" and
  66.  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
  67.  */
  68. static int
  69. fmult(an, srn)
  70.     int        an;
  71.     int        srn;
  72. {
  73.     short        anmag, anexp, anmant;
  74.     short        wanexp, wanmant;
  75.     short        retval;
  76.  
  77.     anmag = (an > 0) ? an : ((-an) & 0x1FFF);
  78.     anexp = quan(anmag, power2, 15) - 6;
  79.     anmant = (anmag == 0) ? 32 :
  80.         (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
  81.     wanexp = anexp + ((srn >> 6) & 0xF) - 13;
  82.  
  83.     wanmant = (anmant * (srn & 077) + 0x30) >> 4;
  84.     retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
  85.         (wanmant >> -wanexp);
  86.  
  87.     return (((an ^ srn) < 0) ? -retval : retval);
  88. }
  89.  
  90. /*
  91.  * g72x_init_state()
  92.  *
  93.  * This routine initializes and/or resets the g72x_state structure
  94.  * pointed to by 'state_ptr'.
  95.  * All the initial state values are specified in the CCITT G.721 document.
  96.  */
  97. void
  98. g72x_init_state(state_ptr)
  99.     struct g72x_state *state_ptr;
  100. {
  101.     int        cnta;
  102.  
  103.     state_ptr->yl = 34816L;
  104.     state_ptr->yu = 544;
  105.     state_ptr->dms = 0;
  106.     state_ptr->dml = 0;
  107.     state_ptr->ap = 0;
  108.     for (cnta = 0; cnta < 2; cnta++) {
  109.         state_ptr->a[cnta] = 0;
  110.         state_ptr->pk[cnta] = 0;
  111.         state_ptr->sr[cnta] = 32;
  112.     }
  113.     for (cnta = 0; cnta < 6; cnta++) {
  114.         state_ptr->b[cnta] = 0;
  115.         state_ptr->dq[cnta] = 32;
  116.     }
  117.     state_ptr->td = 0;
  118. }
  119.  
  120. /*
  121.  * predictor_zero()
  122.  *
  123.  * computes the estimated signal from 6-zero predictor.
  124.  *
  125.  */
  126. int
  127. predictor_zero(state_ptr)
  128.     struct g72x_state *state_ptr;
  129. {
  130.     int        i;
  131.     int        sezi;
  132.  
  133.     sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
  134.     for (i = 1; i < 6; i++)            /* ACCUM */
  135.         sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
  136.     return (sezi);
  137. }
  138. /*
  139.  * predictor_pole()
  140.  *
  141.  * computes the estimated signal from 2-pole predictor.
  142.  *
  143.  */
  144. int
  145. predictor_pole(state_ptr)
  146.     struct g72x_state *state_ptr;
  147. {
  148.     return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
  149.         fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
  150. }
  151. /*
  152.  * step_size()
  153.  *
  154.  * computes the quantization step size of the adaptive quantizer.
  155.  *
  156.  */
  157. int
  158. step_size(state_ptr)
  159.     struct g72x_state *state_ptr;
  160. {
  161.     int        y;
  162.     int        dif;
  163.     int        al;
  164.  
  165.     if (state_ptr->ap >= 256)
  166.         return (state_ptr->yu);
  167.     else {
  168.         y = state_ptr->yl >> 6;
  169.         dif = state_ptr->yu - y;
  170.         al = state_ptr->ap >> 2;
  171.         if (dif > 0)
  172.             y += (dif * al) >> 6;
  173.         else if (dif < 0)
  174.             y += (dif * al + 0x3F) >> 6;
  175.         return (y);
  176.     }
  177. }
  178.  
  179. /*
  180.  * quantize()
  181.  *
  182.  * Given a raw sample, 'd', of the difference signal and a
  183.  * quantization step size scale factor, 'y', this routine returns the
  184.  * ADPCM codeword to which that sample gets quantized.  The step
  185.  * size scale factor division operation is done in the log base 2 domain
  186.  * as a subtraction.
  187.  */
  188. int
  189. quantize(d, y,table,size)
  190.     int        d;    /* Raw difference signal sample */
  191.     int        y;    /* Step size multiplier */
  192.     short        *table;    /* quantization table */
  193.     int        size;    /* table size of short integers */
  194. {
  195.     short        dqm;    /* Magnitude of 'd' */
  196.     short        exp;    /* Integer part of base 2 log of 'd' */
  197.     short        mant;    /* Fractional part of base 2 log */
  198.     short        dl;    /* Log of magnitude of 'd' */
  199.     short        dln;    /* Step size scale factor normalized log */
  200.     int        i;
  201.  
  202.     /*
  203.      * LOG
  204.      *
  205.      * Compute base 2 log of 'd', and store in 'dl'.
  206.      */
  207.     dqm = abs(d);
  208.     exp = quan(dqm >> 1, power2, 15);
  209.     mant = ((dqm << 7) >> exp) & 0x7F;    /* Fractional portion. */
  210.     dl = (exp << 7) + mant;
  211.  
  212.     /*
  213.      * SUBTB
  214.      *
  215.      * "Divide" by step size multiplier.
  216.      */
  217.     dln = dl - (y >> 2);
  218.  
  219.     /*
  220.      * QUAN
  221.      *
  222.      * Obtain codword i for 'd'.
  223.      */
  224.     i = quan(dln, table, size);
  225.     if (d < 0)            /* take 1's complement of i */
  226.         return ((size << 1) + 1 - i);
  227.     else if (i == 0)        /* take 1's complement of 0 */
  228.         return ((size << 1) + 1); /* new in 1988 */
  229.     else
  230.         return (i);
  231. }
  232. /*
  233.  * reconstruct()
  234.  *
  235.  * Returns reconstructed difference signal 'dq' obtained from
  236.  * codeword 'i' and quantization step size scale factor 'y'.
  237.  * Multiplication is performed in log base 2 domain as addition.
  238.  */
  239. int
  240. reconstruct(sign, dqln, y)
  241.     int        sign;    /* 0 for non-negative value */
  242.     int        dqln;    /* G.72x codeword */
  243.     int        y;    /* Step size multiplier */
  244. {
  245.     short        dql;    /* Log of 'dq' magnitude */
  246.     short        dex;    /* Integer part of log */
  247.     short        dqt;
  248.     short        dq;    /* Reconstructed difference signal sample */
  249.  
  250.     dql = dqln + (y >> 2);    /* ADDA */
  251.  
  252.     if (dql < 0) {
  253.         return ((sign) ? -0x8000 : 0);
  254.     } else {        /* ANTILOG */
  255.         dex = (dql >> 7) & 15;
  256.         dqt = 128 + (dql & 127);
  257.         dq = (dqt << 7) >> (14 - dex);
  258.         return ((sign) ? (dq - 0x8000) : dq);
  259.     }
  260. }
  261.  
  262.  
  263. /*
  264.  * update()
  265.  *
  266.  * updates the state variables for each output code
  267.  */
  268. void
  269. update(code_size, y, wi, fi, dq, sr, dqsez, state_ptr)
  270.     int        code_size;    /* distinguish 723_40 with others */
  271.     int        y;        /* quantizer step size */
  272.     int        wi;        /* scale factor multiplier */
  273.     int        fi;        /* for long/short term energies */
  274.     int        dq;        /* quantized prediction difference */
  275.     int        sr;        /* reconstructed signal */
  276.     int        dqsez;        /* difference from 2-pole predictor */
  277.     struct g72x_state *state_ptr;    /* coder state pointer */
  278. {
  279.     int        cnt;
  280.     short        mag, exp;    /* Adaptive predictor, FLOAT A */
  281.     short        a2p=0;        /* LIMC */
  282.     short        a1ul;        /* UPA1 */
  283.     short        pks1;        /* UPA2 */
  284.     short        fa1;
  285.     char        tr;        /* tone/transition detector */
  286.     short        ylint, thr2, dqthr;
  287.     short          ylfrac, thr1;
  288.     short        pk0;
  289.  
  290.     pk0 = (dqsez < 0) ? 1 : 0;    /* needed in updating predictor poles */
  291.  
  292.     mag = dq & 0x7FFF;        /* prediction difference magnitude */
  293.     /* TRANS */
  294.     ylint = state_ptr->yl >> 15;    /* exponent part of yl */
  295.     ylfrac = (state_ptr->yl >> 10) & 0x1F;    /* fractional part of yl */
  296.     thr1 = (32 + ylfrac) << ylint;        /* threshold */
  297.     thr2 = (ylint > 9) ? 31 << 10 : thr1;    /* limit thr2 to 31 << 10 */
  298.     dqthr = (thr2 + (thr2 >> 1)) >> 1;    /* dqthr = 0.75 * thr2 */
  299.     if (state_ptr->td == 0)        /* signal supposed voice */
  300.         tr = 0;
  301.     else if (mag <= dqthr)        /* supposed data, but small mag */
  302.         tr = 0;            /* treated as voice */
  303.     else                /* signal is data (modem) */
  304.         tr = 1;
  305.  
  306.     /*
  307.      * Quantizer scale factor adaptation.
  308.      */
  309.  
  310.     /* FUNCTW & FILTD & DELAY */
  311.     /* update non-steady state step size multiplier */
  312.     state_ptr->yu = y + ((wi - y) >> 5);
  313.  
  314.     /* LIMB */
  315.     if (state_ptr->yu < 544)    /* 544 <= yu <= 5120 */
  316.         state_ptr->yu = 544;
  317.     else if (state_ptr->yu > 5120)
  318.         state_ptr->yu = 5120;
  319.  
  320.     /* FILTE & DELAY */
  321.     /* update steady state step size multiplier */
  322.     state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
  323.  
  324.     /*
  325.      * Adaptive predictor coefficients.
  326.      */
  327.     if (tr == 1) {            /* reset a's and b's for modem signal */
  328.         state_ptr->a[0] = 0;
  329.         state_ptr->a[1] = 0;
  330.         state_ptr->b[0] = 0;
  331.         state_ptr->b[1] = 0;
  332.         state_ptr->b[2] = 0;
  333.         state_ptr->b[3] = 0;
  334.         state_ptr->b[4] = 0;
  335.         state_ptr->b[5] = 0;
  336.     } else {            /* update a's and b's */
  337.         pks1 = pk0 ^ state_ptr->pk[0];        /* UPA2 */
  338.  
  339.         /* update predictor pole a[1] */
  340.         a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
  341.         if (dqsez != 0) {
  342.             fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
  343.             if (fa1 < -8191)    /* a2p = function of fa1 */
  344.                 a2p -= 0x100;
  345.             else if (fa1 > 8191)
  346.                 a2p += 0xFF;
  347.             else
  348.                 a2p += fa1 >> 5;
  349.  
  350.             if (pk0 ^ state_ptr->pk[1])
  351.                 /* LIMC */
  352.                 if (a2p <= -12160)
  353.                     a2p = -12288;
  354.                 else if (a2p >= 12416)
  355.                     a2p = 12288;
  356.                 else
  357.                     a2p -= 0x80;
  358.             else if (a2p <= -12416)
  359.                 a2p = -12288;
  360.             else if (a2p >= 12160)
  361.                 a2p = 12288;
  362.             else
  363.                 a2p += 0x80;
  364.         }
  365.  
  366.         /* Possible bug: a2p not initialized if dqsez == 0) */
  367.         /* TRIGB & DELAY */
  368.         state_ptr->a[1] = a2p;
  369.  
  370.         /* UPA1 */
  371.         /* update predictor pole a[0] */
  372.         state_ptr->a[0] -= state_ptr->a[0] >> 8;
  373.         if (dqsez != 0)
  374.         {
  375.             if (pks1 == 0)
  376.                 state_ptr->a[0] += 192;
  377.             else
  378.                 state_ptr->a[0] -= 192;
  379.         }
  380.         /* LIMD */
  381.         a1ul = 15360 - a2p;
  382.         if (state_ptr->a[0] < -a1ul)
  383.             state_ptr->a[0] = -a1ul;
  384.         else if (state_ptr->a[0] > a1ul)
  385.             state_ptr->a[0] = a1ul;
  386.  
  387.         /* UPB : update predictor zeros b[6] */
  388.         for (cnt = 0; cnt < 6; cnt++) {
  389.             if (code_size == 5)        /* for 40Kbps G.723 */
  390.                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
  391.             else            /* for G.721 and 24Kbps G.723 */
  392.                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
  393.             if (dq & 0x7FFF) {            /* XOR */
  394.                 if ((dq ^ state_ptr->dq[cnt]) >= 0)
  395.                     state_ptr->b[cnt] += 128;
  396.                 else
  397.                     state_ptr->b[cnt] -= 128;
  398.             }
  399.         }
  400.     }
  401.  
  402.     for (cnt = 5; cnt > 0; cnt--)
  403.         state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
  404.     /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
  405.     if (mag == 0) {
  406.         state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
  407.     } else {
  408.         exp = quan(mag, power2, 15);
  409.         state_ptr->dq[0] = (dq >= 0) ?
  410.             (exp << 6) + ((mag << 6) >> exp) :
  411.             (exp << 6) + ((mag << 6) >> exp) - 0x400;
  412.     }
  413.  
  414.     state_ptr->sr[1] = state_ptr->sr[0];
  415.     /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
  416.     if (sr == 0) {
  417.         state_ptr->sr[0] = 0x20;
  418.     } else if (sr > 0) {
  419.         exp = quan(sr, power2, 15);
  420.         state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
  421.     } else if (sr > -32768L) {
  422.         mag = -sr;
  423.         exp = quan(mag, power2, 15);
  424.         state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
  425.     } else
  426.         state_ptr->sr[0] = 0xFC20;
  427.  
  428.     /* DELAY A */
  429.     state_ptr->pk[1] = state_ptr->pk[0];
  430.     state_ptr->pk[0] = pk0;
  431.  
  432.     /* TONE */
  433.     if (tr == 1)        /* this sample has been treated as data */
  434.         state_ptr->td = 0;    /* next one will be treated as voice */
  435.     else if (a2p < -11776)    /* small sample-to-sample correlation */
  436.         state_ptr->td = 1;    /* signal may be data */
  437.     else                /* signal is voice */
  438.         state_ptr->td = 0;
  439.  
  440.     /*
  441.      * Adaptation speed control.
  442.      */
  443.     state_ptr->dms += (fi - state_ptr->dms) >> 5;        /* FILTA */
  444.     state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);    /* FILTB */
  445.  
  446.     if (tr == 1)
  447.         state_ptr->ap = 256;
  448.     else if (y < 1536)                    /* SUBTC */
  449.         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  450.     else if (state_ptr->td == 1)
  451.         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  452.     else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
  453.         (state_ptr->dml >> 3))
  454.         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  455.     else
  456.         state_ptr->ap += (-state_ptr->ap) >> 4;
  457. }
  458.  
  459. /*
  460.  * tandem_adjust(sr, se, y, i, sign)
  461.  *
  462.  * At the end of ADPCM decoding, it simulates an encoder which may be receiving
  463.  * the output of this decoder as a tandem process. If the output of the
  464.  * simulated encoder differs from the input to this decoder, the decoder output
  465.  * is adjusted by one level of A-law or u-law codes.
  466.  *
  467.  * Input:
  468.  *    sr    decoder output linear PCM sample,
  469.  *    se    predictor estimate sample,
  470.  *    y    quantizer step size,
  471.  *    i    decoder input code,
  472.  *    sign    sign bit of code i
  473.  *
  474.  * Return:
  475.  *    adjusted A-law or u-law compressed sample.
  476.  */
  477. int
  478. tandem_adjust_alaw(sr, se, y, i, sign, qtab)
  479.     int        sr;    /* decoder output linear PCM sample */
  480.     int        se;    /* predictor estimate sample */
  481.     int        y;    /* quantizer step size */
  482.     int        i;    /* decoder input code */
  483.     int        sign;
  484.     short        *qtab;
  485. {
  486.     unsigned char    sp;    /* A-law compressed 8-bit code */
  487.     short        dx;    /* prediction error */
  488.     char        id;    /* quantized prediction error */
  489.     int        sd;    /* adjusted A-law decoded sample value */
  490.     int        im;    /* biased magnitude of i */
  491.     int        imx;    /* biased magnitude of id */
  492.  
  493.     if (sr <= -32768L)
  494.         sr = -1;
  495.     sp = st_linear_to_Alaw((sr >> 1) << 3);    /* short to A-law compression */
  496.     dx = (st_Alaw_to_linear(sp) >> 2) - se;    /* 16-bit prediction error */
  497.     id = quantize(dx, y, qtab, sign - 1);
  498.  
  499.     if (id == i) {            /* no adjustment on sp */
  500.         return (sp);
  501.     } else {            /* sp adjustment needed */
  502.         /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
  503.         im = i ^ sign;        /* 2's complement to biased unsigned */
  504.         imx = id ^ sign;
  505.  
  506.         if (imx > im) {        /* sp adjusted to next lower value */
  507.             if (sp & 0x80) {
  508.                 sd = (sp == 0xD5) ? 0x55 :
  509.                     ((sp ^ 0x55) - 1) ^ 0x55;
  510.             } else {
  511.                 sd = (sp == 0x2A) ? 0x2A :
  512.                     ((sp ^ 0x55) + 1) ^ 0x55;
  513.             }
  514.         } else {        /* sp adjusted to next higher value */
  515.             if (sp & 0x80)
  516.                 sd = (sp == 0xAA) ? 0xAA :
  517.                     ((sp ^ 0x55) + 1) ^ 0x55;
  518.             else
  519.                 sd = (sp == 0x55) ? 0xD5 :
  520.                     ((sp ^ 0x55) - 1) ^ 0x55;
  521.         }
  522.         return (sd);
  523.     }
  524. }
  525.  
  526. int
  527. tandem_adjust_ulaw(sr, se, y, i, sign, qtab)
  528.     int        sr;    /* decoder output linear PCM sample */
  529.     int        se;    /* predictor estimate sample */
  530.     int        y;    /* quantizer step size */
  531.     int        i;    /* decoder input code */
  532.     int        sign;
  533.     short        *qtab;
  534. {
  535.     unsigned char    sp;    /* u-law compressed 8-bit code */
  536.     short        dx;    /* prediction error */
  537.     char        id;    /* quantized prediction error */
  538.     int        sd;    /* adjusted u-law decoded sample value */
  539.     int        im;    /* biased magnitude of i */
  540.     int        imx;    /* biased magnitude of id */
  541.  
  542.     if (sr <= -32768L)
  543.         sr = 0;
  544.     sp = st_linear_to_ulaw(sr << 2);    /* short to u-law compression */
  545.     dx = (st_ulaw_to_linear(sp) >> 2) - se;    /* 16-bit prediction error */
  546.     id = quantize(dx, y, qtab, sign - 1);
  547.     if (id == i) {
  548.         return (sp);
  549.     } else {
  550.         /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
  551.         im = i ^ sign;        /* 2's complement to biased unsigned */
  552.         imx = id ^ sign;
  553.         if (imx > im) {        /* sp adjusted to next lower value */
  554.             if (sp & 0x80)
  555.                 sd = (sp == 0xFF) ? 0x7E : sp + 1;
  556.             else
  557.                 sd = (sp == 0) ? 0 : sp - 1;
  558.  
  559.         } else {        /* sp adjusted to next higher value */
  560.             if (sp & 0x80)
  561.                 sd = (sp == 0x80) ? 0x80 : sp - 1;
  562.             else
  563.                 sd = (sp == 0x7F) ? 0xFE : sp + 1;
  564.         }
  565.         return (sd);
  566.     }
  567. }
  568.