g711.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g711.h - In line A-law and u-law conversion routines
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2001 Steve Underwood
  9. *
  10. * Despite my general liking of the GPL, I place this code in the
  11. * public domain for the benefit of all mankind - even the slimy
  12. * ones who might try to proprietize my work and use it to my
  13. * detriment.
  14. *
  15. * $Id: g711.h,v 1.1 2006/06/07 15:46:39 steveu Exp $
  16. *
  17. * Modifications for WebRtc, 2011/04/28, by tlegrand:
  18. * -Changed to use WebRtc types
  19. * -Changed __inline__ to __inline
  20. * -Two changes to make implementation bitexact with ITU-T reference
  21. * implementation
  22. */
  23. /*! \page g711_page A-law and mu-law handling
  24. Lookup tables for A-law and u-law look attractive, until you consider the impact
  25. on the CPU cache. If it causes a substantial area of your processor cache to get
  26. hit too often, cache sloshing will severely slow things down. The main reason
  27. these routines are slow in C, is the lack of direct access to the CPU's "find
  28. the first 1" instruction. A little in-line assembler fixes that, and the
  29. conversion routines can be faster than lookup tables, in most real world usage.
  30. A "find the first 1" instruction is available on most modern CPUs, and is a
  31. much underused feature.
  32. If an assembly language method of bit searching is not available, these routines
  33. revert to a method that can be a little slow, so the cache thrashing might not
  34. seem so bad :(
  35. Feel free to submit patches to add fast "find the first 1" support for your own
  36. favourite processor.
  37. Look up tables are used for transcoding between A-law and u-law, since it is
  38. difficult to achieve the precise transcoding procedure laid down in the G.711
  39. specification by other means.
  40. */
  41. #ifndef MODULES_THIRD_PARTY_G711_G711_H_
  42. #define MODULES_THIRD_PARTY_G711_G711_H_
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. #include <stdint.h>
  47. #if defined(__i386__)
  48. /*! \brief Find the bit position of the highest set bit in a word
  49. \param bits The word to be searched
  50. \return The bit number of the highest set bit, or -1 if the word is zero. */
  51. static __inline__ int top_bit(unsigned int bits) {
  52. int res;
  53. __asm__ __volatile__(
  54. " movl $-1,%%edx;\n"
  55. " bsrl %%eax,%%edx;\n"
  56. : "=d"(res)
  57. : "a"(bits));
  58. return res;
  59. }
  60. /*! \brief Find the bit position of the lowest set bit in a word
  61. \param bits The word to be searched
  62. \return The bit number of the lowest set bit, or -1 if the word is zero. */
  63. static __inline__ int bottom_bit(unsigned int bits) {
  64. int res;
  65. __asm__ __volatile__(
  66. " movl $-1,%%edx;\n"
  67. " bsfl %%eax,%%edx;\n"
  68. : "=d"(res)
  69. : "a"(bits));
  70. return res;
  71. }
  72. #elif defined(__x86_64__)
  73. static __inline__ int top_bit(unsigned int bits) {
  74. int res;
  75. __asm__ __volatile__(
  76. " movq $-1,%%rdx;\n"
  77. " bsrq %%rax,%%rdx;\n"
  78. : "=d"(res)
  79. : "a"(bits));
  80. return res;
  81. }
  82. static __inline__ int bottom_bit(unsigned int bits) {
  83. int res;
  84. __asm__ __volatile__(
  85. " movq $-1,%%rdx;\n"
  86. " bsfq %%rax,%%rdx;\n"
  87. : "=d"(res)
  88. : "a"(bits));
  89. return res;
  90. }
  91. #else
  92. static __inline int top_bit(unsigned int bits) {
  93. int i;
  94. if (bits == 0) {
  95. return -1;
  96. }
  97. i = 0;
  98. if (bits & 0xFFFF0000) {
  99. bits &= 0xFFFF0000;
  100. i += 16;
  101. }
  102. if (bits & 0xFF00FF00) {
  103. bits &= 0xFF00FF00;
  104. i += 8;
  105. }
  106. if (bits & 0xF0F0F0F0) {
  107. bits &= 0xF0F0F0F0;
  108. i += 4;
  109. }
  110. if (bits & 0xCCCCCCCC) {
  111. bits &= 0xCCCCCCCC;
  112. i += 2;
  113. }
  114. if (bits & 0xAAAAAAAA) {
  115. bits &= 0xAAAAAAAA;
  116. i += 1;
  117. }
  118. return i;
  119. }
  120. static __inline int bottom_bit(unsigned int bits) {
  121. int i;
  122. if (bits == 0) {
  123. return -1;
  124. }
  125. i = 32;
  126. if (bits & 0x0000FFFF) {
  127. bits &= 0x0000FFFF;
  128. i -= 16;
  129. }
  130. if (bits & 0x00FF00FF) {
  131. bits &= 0x00FF00FF;
  132. i -= 8;
  133. }
  134. if (bits & 0x0F0F0F0F) {
  135. bits &= 0x0F0F0F0F;
  136. i -= 4;
  137. }
  138. if (bits & 0x33333333) {
  139. bits &= 0x33333333;
  140. i -= 2;
  141. }
  142. if (bits & 0x55555555) {
  143. bits &= 0x55555555;
  144. i -= 1;
  145. }
  146. return i;
  147. }
  148. #endif
  149. /* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
  150. * However, you should consider the cache footprint.
  151. *
  152. * A 64K byte table for linear to x-law and a 512 byte table for x-law to
  153. * linear sound like peanuts these days, and shouldn't an array lookup be
  154. * real fast? No! When the cache sloshes as badly as this one will, a tight
  155. * calculation may be better. The messiest part is normally finding the
  156. * segment, but a little inline assembly can fix that on an i386, x86_64
  157. * and many other modern processors.
  158. */
  159. /*
  160. * Mu-law is basically as follows:
  161. *
  162. * Biased Linear Input Code Compressed Code
  163. * ------------------------ ---------------
  164. * 00000001wxyza 000wxyz
  165. * 0000001wxyzab 001wxyz
  166. * 000001wxyzabc 010wxyz
  167. * 00001wxyzabcd 011wxyz
  168. * 0001wxyzabcde 100wxyz
  169. * 001wxyzabcdef 101wxyz
  170. * 01wxyzabcdefg 110wxyz
  171. * 1wxyzabcdefgh 111wxyz
  172. *
  173. * Each biased linear code has a leading 1 which identifies the segment
  174. * number. The value of the segment number is equal to 7 minus the number
  175. * of leading 0's. The quantization interval is directly available as the
  176. * four bits wxyz. * The trailing bits (a - h) are ignored.
  177. *
  178. * Ordinarily the complement of the resulting code word is used for
  179. * transmission, and so the code word is complemented before it is returned.
  180. *
  181. * For further information see John C. Bellamy's Digital Telephony, 1982,
  182. * John Wiley & Sons, pps 98-111 and 472-476.
  183. */
  184. //#define ULAW_ZEROTRAP /* turn on the trap as per the MIL-STD
  185. //*/
  186. #define ULAW_BIAS 0x84 /* Bias for linear code. */
  187. /*! \brief Encode a linear sample to u-law
  188. \param linear The sample to encode.
  189. \return The u-law value.
  190. */
  191. static __inline uint8_t linear_to_ulaw(int linear) {
  192. uint8_t u_val;
  193. int mask;
  194. int seg;
  195. /* Get the sign and the magnitude of the value. */
  196. if (linear < 0) {
  197. /* WebRtc, tlegrand: -1 added to get bitexact to reference implementation */
  198. linear = ULAW_BIAS - linear - 1;
  199. mask = 0x7F;
  200. } else {
  201. linear = ULAW_BIAS + linear;
  202. mask = 0xFF;
  203. }
  204. seg = top_bit(linear | 0xFF) - 7;
  205. /*
  206. * Combine the sign, segment, quantization bits,
  207. * and complement the code word.
  208. */
  209. if (seg >= 8)
  210. u_val = (uint8_t)(0x7F ^ mask);
  211. else
  212. u_val = (uint8_t)(((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
  213. #ifdef ULAW_ZEROTRAP
  214. /* Optional ITU trap */
  215. if (u_val == 0)
  216. u_val = 0x02;
  217. #endif
  218. return u_val;
  219. }
  220. /*! \brief Decode an u-law sample to a linear value.
  221. \param ulaw The u-law sample to decode.
  222. \return The linear value.
  223. */
  224. static __inline int16_t ulaw_to_linear(uint8_t ulaw) {
  225. int t;
  226. /* Complement to obtain normal u-law value. */
  227. ulaw = ~ulaw;
  228. /*
  229. * Extract and bias the quantization bits. Then
  230. * shift up by the segment number and subtract out the bias.
  231. */
  232. t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int)ulaw & 0x70) >> 4);
  233. return (int16_t)((ulaw & 0x80) ? (ULAW_BIAS - t) : (t - ULAW_BIAS));
  234. }
  235. /*
  236. * A-law is basically as follows:
  237. *
  238. * Linear Input Code Compressed Code
  239. * ----------------- ---------------
  240. * 0000000wxyza 000wxyz
  241. * 0000001wxyza 001wxyz
  242. * 000001wxyzab 010wxyz
  243. * 00001wxyzabc 011wxyz
  244. * 0001wxyzabcd 100wxyz
  245. * 001wxyzabcde 101wxyz
  246. * 01wxyzabcdef 110wxyz
  247. * 1wxyzabcdefg 111wxyz
  248. *
  249. * For further information see John C. Bellamy's Digital Telephony, 1982,
  250. * John Wiley & Sons, pps 98-111 and 472-476.
  251. */
  252. #define ALAW_AMI_MASK 0x55
  253. /*! \brief Encode a linear sample to A-law
  254. \param linear The sample to encode.
  255. \return The A-law value.
  256. */
  257. static __inline uint8_t linear_to_alaw(int linear) {
  258. int mask;
  259. int seg;
  260. if (linear >= 0) {
  261. /* Sign (bit 7) bit = 1 */
  262. mask = ALAW_AMI_MASK | 0x80;
  263. } else {
  264. /* Sign (bit 7) bit = 0 */
  265. mask = ALAW_AMI_MASK;
  266. /* WebRtc, tlegrand: Changed from -8 to -1 to get bitexact to reference
  267. * implementation */
  268. linear = -linear - 1;
  269. }
  270. /* Convert the scaled magnitude to segment number. */
  271. seg = top_bit(linear | 0xFF) - 7;
  272. if (seg >= 8) {
  273. if (linear >= 0) {
  274. /* Out of range. Return maximum value. */
  275. return (uint8_t)(0x7F ^ mask);
  276. }
  277. /* We must be just a tiny step below zero */
  278. return (uint8_t)(0x00 ^ mask);
  279. }
  280. /* Combine the sign, segment, and quantization bits. */
  281. return (uint8_t)(((seg << 4) | ((linear >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^
  282. mask);
  283. }
  284. /*! \brief Decode an A-law sample to a linear value.
  285. \param alaw The A-law sample to decode.
  286. \return The linear value.
  287. */
  288. static __inline int16_t alaw_to_linear(uint8_t alaw) {
  289. int i;
  290. int seg;
  291. alaw ^= ALAW_AMI_MASK;
  292. i = ((alaw & 0x0F) << 4);
  293. seg = (((int)alaw & 0x70) >> 4);
  294. if (seg)
  295. i = (i + 0x108) << (seg - 1);
  296. else
  297. i += 8;
  298. return (int16_t)((alaw & 0x80) ? i : -i);
  299. }
  300. /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
  301. \param alaw The A-law sample to transcode.
  302. \return The best matching u-law value.
  303. */
  304. uint8_t alaw_to_ulaw(uint8_t alaw);
  305. /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
  306. \param alaw The u-law sample to transcode.
  307. \return The best matching A-law value.
  308. */
  309. uint8_t ulaw_to_alaw(uint8_t ulaw);
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* MODULES_THIRD_PARTY_G711_G711_H_ */