g711.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Version 3.01 - 31.Jan.2000
  2. =============================================================================
  3. U U GGG SSS TTTTT
  4. U U G S T
  5. U U G GG SSS T
  6. U U G G S T
  7. UUUU GGG SSS T
  8. ========================================
  9. ITU-T - USER'S GROUP ON SOFTWARE TOOLS
  10. ========================================
  11. =============================================================
  12. COPYRIGHT NOTE: This source code, and all of its derivations,
  13. is subject to the "ITU-T General Public License". Please have
  14. it read in the distribution disk, or in the ITU-T
  15. Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
  16. CODING STANDARDS".
  17. =============================================================
  18. MODULE: G711.C, G.711 ENCODING/DECODING FUNCTIONS
  19. ORIGINAL BY:
  20. Simao Ferraz de Campos Neto Rudolf Hofmann
  21. CPqD/Telebras PHILIPS KOMMUNIKATIONS INDUSTRIE AG
  22. DDS/Pr.11 Kommunikationssysteme
  23. Rd. Mogi Mirim-Campinas Km.118 Thurn-und-Taxis-Strasse 14
  24. 13.085 - Campinas - SP (Brazil) D-8500 Nuernberg 10 (Germany)
  25. Phone : +55-192-39-6396 Phone : +49 911 526-2603
  26. FAX : +55-192-53-4754 FAX : +49 911 526-3385
  27. EMail : tdsimao@venus.cpqd.ansp.br EMail : HF@PKINBG.UUCP
  28. FUNCTIONS:
  29. alaw_compress: ... compands 1 vector of linear PCM samples to A-law;
  30. uses 13 Most Sig.Bits (MSBs) from input and 8 Least
  31. Sig. Bits (LSBs) on output.
  32. alaw_expand: ..... expands 1 vector of A-law samples to linear PCM;
  33. use 8 Least Sig. Bits (LSBs) from input and
  34. 13 Most Sig.Bits (MSBs) on output.
  35. ulaw_compress: ... compands 1 vector of linear PCM samples to u-law;
  36. uses 14 Most Sig.Bits (MSBs) from input and 8 Least
  37. Sig. Bits (LSBs) on output.
  38. ulaw_expand: ..... expands 1 vector of u-law samples to linear PCM
  39. use 8 Least Sig. Bits (LSBs) from input and
  40. 14 Most Sig.Bits (MSBs) on output.
  41. PROTOTYPES: in g711.h
  42. HISTORY:
  43. Apr/91 1.0 First version of the G711 module
  44. 10/Dec/1991 2.0 Break-up in individual functions for A,u law;
  45. correction of bug in compression routines (use of 1
  46. and 2 complement); Demo program inside module.
  47. 08/Feb/1992 3.0 Demo as separate file;
  48. 31/Jan/2000 3.01 Updated documentation text; no change in functions
  49. <simao.campos@labs.comsat.com>
  50. =============================================================================
  51. */
  52. /*
  53. * .......... I N C L U D E S ..........
  54. */
  55. /* Global prototype functions */
  56. #include "g711.h"
  57. /*
  58. * .......... F U N C T I O N S ..........
  59. */
  60. /* ................... Begin of alaw_compress() ..................... */
  61. /*
  62. ==========================================================================
  63. FUNCTION NAME: alaw_compress
  64. DESCRIPTION: ALaw encoding rule according ITU-T Rec. G.711.
  65. PROTOTYPE: void alaw_compress(long lseg, short *linbuf, short *logbuf)
  66. PARAMETERS:
  67. lseg: (In) number of samples
  68. linbuf: (In) buffer with linear samples (only 12 MSBits are taken
  69. into account)
  70. logbuf: (Out) buffer with compressed samples (8 bit right justified,
  71. without sign extension)
  72. RETURN VALUE: none.
  73. HISTORY:
  74. 10.Dec.91 1.0 Separated A-law compression function
  75. ==========================================================================
  76. */
  77. void alaw_compress (long lseg, short *linbuf, short *logbuf) {
  78. short ix, iexp;
  79. long n;
  80. for (n = 0; n < lseg; n++) {
  81. ix = linbuf[n] < 0 /* 0 <= ix < 2048 */
  82. ? (~linbuf[n]) >> 4 /* 1's complement for negative values */
  83. : (linbuf[n]) >> 4;
  84. /* Do more, if exponent > 0 */
  85. if (ix > 15) { /* exponent=0 for ix <= 15 */
  86. iexp = 1; /* first step: */
  87. while (ix > 16 + 15) { /* find mantissa and exponent */
  88. ix >>= 1;
  89. iexp++;
  90. }
  91. ix -= 16; /* second step: remove leading '1' */
  92. ix += iexp << 4; /* now compute encoded value */
  93. }
  94. if (linbuf[n] >= 0)
  95. ix |= (0x0080); /* add sign bit */
  96. logbuf[n] = ix ^ (0x0055); /* toggle even bits */
  97. }
  98. }
  99. /* ................... End of alaw_compress() ..................... */
  100. /* ................... Begin of alaw_expand() ..................... */
  101. /*
  102. ==========================================================================
  103. FUNCTION NAME: alaw_expand
  104. DESCRIPTION: ALaw decoding rule according ITU-T Rec. G.711.
  105. PROTOTYPE: void alaw_expand(long lseg, short *logbuf, short *linbuf)
  106. PARAMETERS:
  107. lseg: (In) number of samples
  108. logbuf: (In) buffer with compressed samples (8 bit right justified,
  109. without sign extension)
  110. linbuf: (Out) buffer with linear samples (13 bits left justified)
  111. RETURN VALUE: none.
  112. HISTORY:
  113. 10.Dec.91 1.0 Separated A-law expansion function
  114. ============================================================================
  115. */
  116. void alaw_expand (long lseg, short *logbuf, short *linbuf) {
  117. short ix, mant, iexp;
  118. long n;
  119. for (n = 0; n < lseg; n++) {
  120. ix = logbuf[n] ^ (0x0055); /* re-toggle toggled bits */
  121. ix &= (0x007F); /* remove sign bit */
  122. iexp = ix >> 4; /* extract exponent */
  123. mant = ix & (0x000F); /* now get mantissa */
  124. if (iexp > 0)
  125. mant = mant + 16; /* add leading '1', if exponent > 0 */
  126. mant = (mant << 4) + (0x0008); /* now mantissa left justified and */
  127. /* 1/2 quantization step added */
  128. if (iexp > 1) /* now left shift according exponent */
  129. mant = mant << (iexp - 1);
  130. linbuf[n] = logbuf[n] > 127 /* invert, if negative sample */
  131. ? mant : -mant;
  132. }
  133. }
  134. /* ................... End of alaw_expand() ..................... */
  135. /* ................... Begin of ulaw_compress() ..................... */
  136. /*
  137. ==========================================================================
  138. FUNCTION NAME: ulaw_compress
  139. DESCRIPTION: Mu law encoding rule according ITU-T Rec. G.711.
  140. PROTOTYPE: void ulaw_compress(long lseg, short *linbuf, short *logbuf)
  141. PARAMETERS:
  142. lseg: (In) number of samples
  143. linbuf: (In) buffer with linear samples (only 12 MSBits are taken
  144. into account)
  145. logbuf: (Out) buffer with compressed samples (8 bit right justified,
  146. without sign extension)
  147. RETURN VALUE: none.
  148. HISTORY:
  149. 10.Dec.91 1.0 Separated mu-law compression function
  150. ==========================================================================
  151. */
  152. void ulaw_compress (long lseg, short *linbuf, short *logbuf) {
  153. long n; /* samples's count */
  154. short i; /* aux.var. */
  155. short absno; /* absolute value of linear (input) sample */
  156. short segno; /* segment (Table 2/G711, column 1) */
  157. short low_nibble; /* low nibble of log companded sample */
  158. short high_nibble; /* high nibble of log companded sample */
  159. for (n = 0; n < lseg; n++) {
  160. /* -------------------------------------------------------------------- */
  161. /* Change from 14 bit left justified to 14 bit right justified */
  162. /* Compute absolute value; adjust for easy processing */
  163. /* -------------------------------------------------------------------- */
  164. absno = linbuf[n] < 0 /* compute 1's complement in case of */
  165. ? ((~linbuf[n]) >> 2) + 33 /* negative samples */
  166. : ((linbuf[n]) >> 2) + 33; /* NB: 33 is the difference value */
  167. /* between the thresholds for */
  168. /* A-law and u-law. */
  169. if (absno > (0x1FFF)) /* limitation to "absno" < 8192 */
  170. absno = (0x1FFF);
  171. /* Determination of sample's segment */
  172. i = absno >> 6;
  173. segno = 1;
  174. while (i != 0) {
  175. segno++;
  176. i >>= 1;
  177. }
  178. /* Mounting the high-nibble of the log-PCM sample */
  179. high_nibble = (0x0008) - segno;
  180. /* Mounting the low-nibble of the log PCM sample */
  181. low_nibble = (absno >> segno) /* right shift of mantissa and */
  182. &(0x000F); /* masking away leading '1' */
  183. low_nibble = (0x000F) - low_nibble;
  184. /* Joining the high-nibble and the low-nibble of the log PCM sample */
  185. logbuf[n] = (high_nibble << 4) | low_nibble;
  186. /* Add sign bit */
  187. if (linbuf[n] >= 0)
  188. logbuf[n] = logbuf[n] | (0x0080);
  189. }
  190. }
  191. /* ................... End of ulaw_compress() ..................... */
  192. /* ................... Begin of ulaw_expand() ..................... */
  193. /*
  194. ==========================================================================
  195. FUNCTION NAME: ulaw_expand
  196. DESCRIPTION: Mu law decoding rule according ITU-T Rec. G.711.
  197. PROTOTYPE: void ulaw_expand(long lseg, short *logbuf, short *linbuf)
  198. PARAMETERS:
  199. lseg: (In) number of samples
  200. logbuf: (In) buffer with compressed samples (8 bit right justified,
  201. without sign extension)
  202. linbuf: (Out) buffer with linear samples (14 bits left justified)
  203. RETURN VALUE: none.
  204. HISTORY:
  205. 10.Dec.91 1.0 Separated mu law expansion function
  206. ============================================================================
  207. */
  208. void ulaw_expand (long lseg, short *logbuf, short *linbuf) {
  209. long n; /* aux.var. */
  210. short segment; /* segment (Table 2/G711, column 1) */
  211. short mantissa; /* low nibble of log companded sample */
  212. short exponent; /* high nibble of log companded sample */
  213. short sign; /* sign of output sample */
  214. short step;
  215. for (n = 0; n < lseg; n++) {
  216. sign = logbuf[n] < (0x0080) /* sign-bit = 1 for positiv values */
  217. ? -1 : 1;
  218. mantissa = ~logbuf[n]; /* 1's complement of input value */
  219. exponent = (mantissa >> 4) & (0x0007); /* extract exponent */
  220. segment = exponent + 1; /* compute segment number */
  221. mantissa = mantissa & (0x000F); /* extract mantissa */
  222. /* Compute Quantized Sample (14 bit left justified!) */
  223. step = (4) << segment; /* position of the LSB */
  224. /* = 1 quantization step) */
  225. linbuf[n] = sign * /* sign */
  226. (((0x0080) << exponent) /* '1', preceding the mantissa */
  227. +step * mantissa /* left shift of mantissa */
  228. + step / 2 /* 1/2 quantization step */
  229. - 4 * 33);
  230. }
  231. }
  232. /* ................... End of ulaw_expand() ..................... */