g711.c 8.4 KB

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