You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.2 KiB

  1. /* Mu-law code from Linux kernel
  2. * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
  3. * Uros Bizjak <uros@kss-loka.si>
  4. * Copyright Jeroen Vreeken (jeroen@vreeken.net), 2017
  5. *
  6. * Based on reference implementation by Sun Microsystems, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Library General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include "ulaw.h"
  24. #define BIAS (0x84) /* Bias for linear code. */
  25. #define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */
  26. #define QUANT_MASK (0xf) /* Quantization field mask. */
  27. #define NSEGS (8) /* Number of u-law segments. */
  28. #define SEG_SHIFT (4) /* Left shift for segment number. */
  29. #define SEG_MASK (0x70) /* Segment field mask. */
  30. static inline int val_seg(int val)
  31. {
  32. int r = 0;
  33. val >>= 7;
  34. if (val & 0xf0) {
  35. val >>= 4;
  36. r += 4;
  37. }
  38. if (val & 0x0c) {
  39. val >>= 2;
  40. r += 2;
  41. }
  42. if (val & 0x02)
  43. r += 1;
  44. return r;
  45. }
  46. /*
  47. * linear2ulaw() - Convert a linear PCM value to u-law
  48. *
  49. * In order to simplify the encoding process, the original linear magnitude
  50. * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
  51. * (33 - 8191). The result can be seen in the following encoding table:
  52. *
  53. * Biased Linear Input Code Compressed Code
  54. * ------------------------ ---------------
  55. * 00000001wxyza 000wxyz
  56. * 0000001wxyzab 001wxyz
  57. * 000001wxyzabc 010wxyz
  58. * 00001wxyzabcd 011wxyz
  59. * 0001wxyzabcde 100wxyz
  60. * 001wxyzabcdef 101wxyz
  61. * 01wxyzabcdefg 110wxyz
  62. * 1wxyzabcdefgh 111wxyz
  63. *
  64. * Each biased linear code has a leading 1 which identifies the segment
  65. * number. The value of the segment number is equal to 7 minus the number
  66. * of leading 0's. The quantization interval is directly available as the
  67. * four bits wxyz. * The trailing bits (a - h) are ignored.
  68. *
  69. * Ordinarily the complement of the resulting code word is used for
  70. * transmission, and so the code word is complemented before it is returned.
  71. *
  72. * For further information see John C. Bellamy's Digital Telephony, 1982,
  73. * John Wiley & Sons, pps 98-111 and 472-476.
  74. */
  75. static uint8_t linear2ulaw(int16_t pcm_val) /* 2's complement (16-bit range) */
  76. {
  77. int mask;
  78. int seg;
  79. unsigned char uval;
  80. /* Get the sign and the magnitude of the value. */
  81. if (pcm_val < 0) {
  82. pcm_val = BIAS - pcm_val;
  83. mask = 0x7F;
  84. } else {
  85. pcm_val += BIAS;
  86. mask = 0xFF;
  87. }
  88. if (pcm_val > 0x7FFF)
  89. pcm_val = 0x7FFF;
  90. /* Convert the scaled magnitude to segment number. */
  91. seg = val_seg(pcm_val);
  92. /*
  93. * Combine the sign, segment, quantization bits;
  94. * and complement the code word.
  95. */
  96. uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  97. return uval ^ mask;
  98. }
  99. /*
  100. * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
  101. *
  102. * First, a biased linear code is derived from the code word. An unbiased
  103. * output can then be obtained by subtracting 33 from the biased code.
  104. *
  105. * Note that this function expects to be passed the complement of the
  106. * original code word. This is in keeping with ISDN conventions.
  107. */
  108. static int16_t ulaw2linear(uint8_t u_val)
  109. {
  110. int t;
  111. /* Complement to obtain normal u-law value. */
  112. u_val = ~u_val;
  113. /*
  114. * Extract and bias the quantization bits. Then
  115. * shift up by the segment number and subtract out the bias.
  116. */
  117. t = ((u_val & QUANT_MASK) << 3) + BIAS;
  118. t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
  119. return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
  120. }
  121. void ulaw_decode(int16_t *samples, uint8_t *ulaw, int nr)
  122. {
  123. int i;
  124. for (i = 0; i < nr; i++) {
  125. samples[i] = ulaw2linear(ulaw[i]);
  126. }
  127. }
  128. void ulaw_encode(uint8_t *ulaw, int16_t *samples, int nr)
  129. {
  130. int i;
  131. for (i = 0; i < nr; i++) {
  132. ulaw[i] = linear2ulaw(samples[i]);
  133. }
  134. }