home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / g711.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  8KB  |  289 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.  * g711.c
  29.  *
  30.  * u-law, A-law and linear PCM conversions.
  31.  */
  32. #define    SIGN_BIT    (0x80)        /* Sign bit for a A-law byte. */
  33. #define    QUANT_MASK    (0xf)        /* Quantization field mask. */
  34. #define    NSEGS        (8)        /* Number of A-law segments. */
  35. #define    SEG_SHIFT    (4)        /* Left shift for segment number. */
  36. #define    SEG_MASK    (0x70)        /* Segment field mask. */
  37.  
  38. /* copy from CCITT G.711 specifications */
  39. unsigned char _u2a[128] = {            /* u- to A-law conversions */
  40.     1,    1,    2,    2,    3,    3,    4,    4,
  41.     5,    5,    6,    6,    7,    7,    8,    8,
  42.     9,    10,    11,    12,    13,    14,    15,    16,
  43.     17,    18,    19,    20,    21,    22,    23,    24,
  44.     25,    27,    29,    31,    33,    34,    35,    36,
  45.     37,    38,    39,    40,    41,    42,    43,    44,
  46.     46,    48,    49,    50,    51,    52,    53,    54,
  47.     55,    56,    57,    58,    59,    60,    61,    62,
  48.     64,    65,    66,    67,    68,    69,    70,    71,
  49.     72,    73,    74,    75,    76,    77,    78,    79,
  50.     81,    82,    83,    84,    85,    86,    87,    88,
  51.     89,    90,    91,    92,    93,    94,    95,    96,
  52.     97,    98,    99,    100,    101,    102,    103,    104,
  53.     105,    106,    107,    108,    109,    110,    111,    112,
  54.     113,    114,    115,    116,    117,    118,    119,    120,
  55.     121,    122,    123,    124,    125,    126,    127,    128};
  56.  
  57. unsigned char _a2u[128] = {            /* A- to u-law conversions */
  58.     1,    3,    5,    7,    9,    11,    13,    15,
  59.     16,    17,    18,    19,    20,    21,    22,    23,
  60.     24,    25,    26,    27,    28,    29,    30,    31,
  61.     32,    32,    33,    33,    34,    34,    35,    35,
  62.     36,    37,    38,    39,    40,    41,    42,    43,
  63.     44,    45,    46,    47,    48,    48,    49,    49,
  64.     50,    51,    52,    53,    54,    55,    56,    57,
  65.     58,    59,    60,    61,    62,    63,    64,    64,
  66.     65,    66,    67,    68,    69,    70,    71,    72,
  67.     73,    74,    75,    76,    77,    78,    79,    79,
  68.     80,    81,    82,    83,    84,    85,    86,    87,
  69.     88,    89,    90,    91,    92,    93,    94,    95,
  70.     96,    97,    98,    99,    100,    101,    102,    103,
  71.     104,    105,    106,    107,    108,    109,    110,    111,
  72.     112,    113,    114,    115,    116,    117,    118,    119,
  73.     120,    121,    122,    123,    124,    125,    126,    127};
  74.  
  75. /* see libst.h */
  76. #ifdef    SUPERCEDED
  77.  
  78. static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
  79.                 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  80.  
  81. static int
  82. search(val, table, size)
  83.     int        val;
  84.     short        *table;
  85.     int        size;
  86. {
  87.     int        i;
  88.  
  89.     for (i = 0; i < size; i++) {
  90.         if (val <= *table++)
  91.             return (i);
  92.     }
  93.     return (size);
  94. }
  95.  
  96. /*
  97.  * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
  98.  *
  99.  * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
  100.  *
  101.  *        Linear Input Code    Compressed Code
  102.  *    ------------------------    ---------------
  103.  *    0000000wxyza            000wxyz
  104.  *    0000001wxyza            001wxyz
  105.  *    000001wxyzab            010wxyz
  106.  *    00001wxyzabc            011wxyz
  107.  *    0001wxyzabcd            100wxyz
  108.  *    001wxyzabcde            101wxyz
  109.  *    01wxyzabcdef            110wxyz
  110.  *    1wxyzabcdefg            111wxyz
  111.  *
  112.  * For further information see John C. Bellamy's Digital Telephony, 1982,
  113.  * John Wiley & Sons, pps 98-111 and 472-476.
  114.  */
  115. unsigned char
  116. linear2alaw(pcm_val)
  117.     int        pcm_val;    /* 2's complement (16-bit range) */
  118. {
  119.     int        mask;
  120.     int        seg;
  121.     unsigned char    aval;
  122.  
  123.     if (pcm_val >= 0) {
  124.         mask = 0xD5;        /* sign (7th) bit = 1 */
  125.     } else {
  126.         mask = 0x55;        /* sign bit = 0 */
  127.         pcm_val = -pcm_val - 8;
  128.     }
  129.  
  130.     /* Convert the scaled magnitude to segment number. */
  131.     seg = search(pcm_val, seg_end, 8);
  132.  
  133.     /* Combine the sign, segment, and quantization bits. */
  134.  
  135.     if (seg >= 8)        /* out of range, return maximum value. */
  136.         return (0x7F ^ mask);
  137.     else {
  138.         aval = seg << SEG_SHIFT;
  139.         if (seg < 2)
  140.             aval |= (pcm_val >> 4) & QUANT_MASK;
  141.         else
  142.             aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
  143.         return (aval ^ mask);
  144.     }
  145. }
  146.  
  147. /*
  148.  * alaw2linear() - Convert an A-law value to 16-bit linear PCM
  149.  *
  150.  */
  151. int
  152. alaw2linear(a_val)
  153.     unsigned char    a_val;
  154. {
  155.     int        t;
  156.     int        seg;
  157.  
  158.     a_val ^= 0x55;
  159.  
  160.     t = (a_val & QUANT_MASK) << 4;
  161.     seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
  162.     switch (seg) {
  163.     case 0:
  164.         t += 8;
  165.         break;
  166.     case 1:
  167.         t += 0x108;
  168.         break;
  169.     default:
  170.         t += 0x108;
  171.         t <<= seg - 1;
  172.     }
  173.     return ((a_val & SIGN_BIT) ? t : -t);
  174. }
  175.  
  176. #define    BIAS        (0x84)        /* Bias for linear code. */
  177.  
  178. /*
  179.  * linear2ulaw() - Convert a linear PCM value to u-law
  180.  *
  181.  * In order to simplify the encoding process, the original linear magnitude
  182.  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
  183.  * (33 - 8191). The result can be seen in the following encoding table:
  184.  *
  185.  *    Biased Linear Input Code    Compressed Code
  186.  *    ------------------------    ---------------
  187.  *    00000001wxyza            000wxyz
  188.  *    0000001wxyzab            001wxyz
  189.  *    000001wxyzabc            010wxyz
  190.  *    00001wxyzabcd            011wxyz
  191.  *    0001wxyzabcde            100wxyz
  192.  *    001wxyzabcdef            101wxyz
  193.  *    01wxyzabcdefg            110wxyz
  194.  *    1wxyzabcdefgh            111wxyz
  195.  *
  196.  * Each biased linear code has a leading 1 which identifies the segment
  197.  * number. The value of the segment number is equal to 7 minus the number
  198.  * of leading 0's. The quantization interval is directly available as the
  199.  * four bits wxyz.  * The trailing bits (a - h) are ignored.
  200.  *
  201.  * Ordinarily the complement of the resulting code word is used for
  202.  * transmission, and so the code word is complemented before it is returned.
  203.  *
  204.  * For further information see John C. Bellamy's Digital Telephony, 1982,
  205.  * John Wiley & Sons, pps 98-111 and 472-476.
  206.  */
  207. unsigned char
  208. linear2ulaw(pcm_val)
  209.     int        pcm_val;    /* 2's complement (16-bit range) */
  210. {
  211.     int        mask;
  212.     int        seg;
  213.     unsigned char    uval;
  214.  
  215.     /* Get the sign and the magnitude of the value. */
  216.     if (pcm_val < 0) {
  217.         pcm_val = BIAS - pcm_val;
  218.         mask = 0x7F;
  219.     } else {
  220.         pcm_val += BIAS;
  221.         mask = 0xFF;
  222.     }
  223.  
  224.     /* Convert the scaled magnitude to segment number. */
  225.     seg = search(pcm_val, seg_end, 8);
  226.  
  227.     /*
  228.      * Combine the sign, segment, quantization bits;
  229.      * and complement the code word.
  230.      */
  231.     if (seg >= 8)        /* out of range, return maximum value. */
  232.         return (0x7F ^ mask);
  233.     else {
  234.         uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  235.         return (uval ^ mask);
  236.     }
  237.  
  238. }
  239.  
  240. /*
  241.  * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
  242.  *
  243.  * First, a biased linear code is derived from the code word. An unbiased
  244.  * output can then be obtained by subtracting 33 from the biased code.
  245.  *
  246.  * Note that this function expects to be passed the complement of the
  247.  * original code word. This is in keeping with ISDN conventions.
  248.  */
  249. int
  250. ulaw2linear(u_val)
  251.     unsigned char    u_val;
  252. {
  253.     int        t;
  254.  
  255.     /* Complement to obtain normal u-law value. */
  256.     u_val = ~u_val;
  257.  
  258.     /*
  259.      * Extract and bias the quantization bits. Then
  260.      * shift up by the segment number and subtract out the bias.
  261.      */
  262.     t = ((u_val & QUANT_MASK) << 3) + BIAS;
  263.     t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
  264.  
  265.     return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
  266. }
  267.  
  268. #endif
  269.  
  270. /* A-law to u-law conversion */
  271. unsigned char
  272. alaw2ulaw(aval)
  273.     unsigned char    aval;
  274. {
  275.     aval &= 0xff;
  276.     return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
  277.         (0x7F ^ _a2u[aval ^ 0x55]));
  278. }
  279.  
  280. /* u-law to A-law conversion */
  281. unsigned char
  282. ulaw2alaw(uval)
  283.     unsigned char    uval;
  284. {
  285.     uval &= 0xff;
  286.     return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
  287.         (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
  288. }
  289.