mulaw.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* $OpenBSD: mulaw.c,v 1.18 2015/06/25 06:43:46 ratchov Exp $ */
  2. /* $NetBSD: mulaw.c,v 1.15 2001/01/18 20:28:20 jdolecek Exp $ */
  3. /*
  4. * Copyright (c) 1991-1993 Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by the Computer Systems
  18. * Engineering Group at Lawrence Berkeley Laboratory.
  19. * 4. Neither the name of the University nor of the Laboratory may be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. */
  36. #include <sys/types.h>
  37. #include <dev/mulaw.h>
  38. /*
  39. * This table converts a (8 bit) mu-law value two a 16 bit value.
  40. * The 16 bits are represented as an array of two bytes for easier access
  41. * to the individual bytes.
  42. */
  43. static const u_char mulawtolin16[256][2] = {
  44. {0x02,0x84}, {0x06,0x84}, {0x0a,0x84}, {0x0e,0x84},
  45. {0x12,0x84}, {0x16,0x84}, {0x1a,0x84}, {0x1e,0x84},
  46. {0x22,0x84}, {0x26,0x84}, {0x2a,0x84}, {0x2e,0x84},
  47. {0x32,0x84}, {0x36,0x84}, {0x3a,0x84}, {0x3e,0x84},
  48. {0x41,0x84}, {0x43,0x84}, {0x45,0x84}, {0x47,0x84},
  49. {0x49,0x84}, {0x4b,0x84}, {0x4d,0x84}, {0x4f,0x84},
  50. {0x51,0x84}, {0x53,0x84}, {0x55,0x84}, {0x57,0x84},
  51. {0x59,0x84}, {0x5b,0x84}, {0x5d,0x84}, {0x5f,0x84},
  52. {0x61,0x04}, {0x62,0x04}, {0x63,0x04}, {0x64,0x04},
  53. {0x65,0x04}, {0x66,0x04}, {0x67,0x04}, {0x68,0x04},
  54. {0x69,0x04}, {0x6a,0x04}, {0x6b,0x04}, {0x6c,0x04},
  55. {0x6d,0x04}, {0x6e,0x04}, {0x6f,0x04}, {0x70,0x04},
  56. {0x70,0xc4}, {0x71,0x44}, {0x71,0xc4}, {0x72,0x44},
  57. {0x72,0xc4}, {0x73,0x44}, {0x73,0xc4}, {0x74,0x44},
  58. {0x74,0xc4}, {0x75,0x44}, {0x75,0xc4}, {0x76,0x44},
  59. {0x76,0xc4}, {0x77,0x44}, {0x77,0xc4}, {0x78,0x44},
  60. {0x78,0xa4}, {0x78,0xe4}, {0x79,0x24}, {0x79,0x64},
  61. {0x79,0xa4}, {0x79,0xe4}, {0x7a,0x24}, {0x7a,0x64},
  62. {0x7a,0xa4}, {0x7a,0xe4}, {0x7b,0x24}, {0x7b,0x64},
  63. {0x7b,0xa4}, {0x7b,0xe4}, {0x7c,0x24}, {0x7c,0x64},
  64. {0x7c,0x94}, {0x7c,0xb4}, {0x7c,0xd4}, {0x7c,0xf4},
  65. {0x7d,0x14}, {0x7d,0x34}, {0x7d,0x54}, {0x7d,0x74},
  66. {0x7d,0x94}, {0x7d,0xb4}, {0x7d,0xd4}, {0x7d,0xf4},
  67. {0x7e,0x14}, {0x7e,0x34}, {0x7e,0x54}, {0x7e,0x74},
  68. {0x7e,0x8c}, {0x7e,0x9c}, {0x7e,0xac}, {0x7e,0xbc},
  69. {0x7e,0xcc}, {0x7e,0xdc}, {0x7e,0xec}, {0x7e,0xfc},
  70. {0x7f,0x0c}, {0x7f,0x1c}, {0x7f,0x2c}, {0x7f,0x3c},
  71. {0x7f,0x4c}, {0x7f,0x5c}, {0x7f,0x6c}, {0x7f,0x7c},
  72. {0x7f,0x88}, {0x7f,0x90}, {0x7f,0x98}, {0x7f,0xa0},
  73. {0x7f,0xa8}, {0x7f,0xb0}, {0x7f,0xb8}, {0x7f,0xc0},
  74. {0x7f,0xc8}, {0x7f,0xd0}, {0x7f,0xd8}, {0x7f,0xe0},
  75. {0x7f,0xe8}, {0x7f,0xf0}, {0x7f,0xf8}, {0x80,0x00},
  76. {0xfd,0x7c}, {0xf9,0x7c}, {0xf5,0x7c}, {0xf1,0x7c},
  77. {0xed,0x7c}, {0xe9,0x7c}, {0xe5,0x7c}, {0xe1,0x7c},
  78. {0xdd,0x7c}, {0xd9,0x7c}, {0xd5,0x7c}, {0xd1,0x7c},
  79. {0xcd,0x7c}, {0xc9,0x7c}, {0xc5,0x7c}, {0xc1,0x7c},
  80. {0xbe,0x7c}, {0xbc,0x7c}, {0xba,0x7c}, {0xb8,0x7c},
  81. {0xb6,0x7c}, {0xb4,0x7c}, {0xb2,0x7c}, {0xb0,0x7c},
  82. {0xae,0x7c}, {0xac,0x7c}, {0xaa,0x7c}, {0xa8,0x7c},
  83. {0xa6,0x7c}, {0xa4,0x7c}, {0xa2,0x7c}, {0xa0,0x7c},
  84. {0x9e,0xfc}, {0x9d,0xfc}, {0x9c,0xfc}, {0x9b,0xfc},
  85. {0x9a,0xfc}, {0x99,0xfc}, {0x98,0xfc}, {0x97,0xfc},
  86. {0x96,0xfc}, {0x95,0xfc}, {0x94,0xfc}, {0x93,0xfc},
  87. {0x92,0xfc}, {0x91,0xfc}, {0x90,0xfc}, {0x8f,0xfc},
  88. {0x8f,0x3c}, {0x8e,0xbc}, {0x8e,0x3c}, {0x8d,0xbc},
  89. {0x8d,0x3c}, {0x8c,0xbc}, {0x8c,0x3c}, {0x8b,0xbc},
  90. {0x8b,0x3c}, {0x8a,0xbc}, {0x8a,0x3c}, {0x89,0xbc},
  91. {0x89,0x3c}, {0x88,0xbc}, {0x88,0x3c}, {0x87,0xbc},
  92. {0x87,0x5c}, {0x87,0x1c}, {0x86,0xdc}, {0x86,0x9c},
  93. {0x86,0x5c}, {0x86,0x1c}, {0x85,0xdc}, {0x85,0x9c},
  94. {0x85,0x5c}, {0x85,0x1c}, {0x84,0xdc}, {0x84,0x9c},
  95. {0x84,0x5c}, {0x84,0x1c}, {0x83,0xdc}, {0x83,0x9c},
  96. {0x83,0x6c}, {0x83,0x4c}, {0x83,0x2c}, {0x83,0x0c},
  97. {0x82,0xec}, {0x82,0xcc}, {0x82,0xac}, {0x82,0x8c},
  98. {0x82,0x6c}, {0x82,0x4c}, {0x82,0x2c}, {0x82,0x0c},
  99. {0x81,0xec}, {0x81,0xcc}, {0x81,0xac}, {0x81,0x8c},
  100. {0x81,0x74}, {0x81,0x64}, {0x81,0x54}, {0x81,0x44},
  101. {0x81,0x34}, {0x81,0x24}, {0x81,0x14}, {0x81,0x04},
  102. {0x80,0xf4}, {0x80,0xe4}, {0x80,0xd4}, {0x80,0xc4},
  103. {0x80,0xb4}, {0x80,0xa4}, {0x80,0x94}, {0x80,0x84},
  104. {0x80,0x78}, {0x80,0x70}, {0x80,0x68}, {0x80,0x60},
  105. {0x80,0x58}, {0x80,0x50}, {0x80,0x48}, {0x80,0x40},
  106. {0x80,0x38}, {0x80,0x30}, {0x80,0x28}, {0x80,0x20},
  107. {0x80,0x18}, {0x80,0x10}, {0x80,0x08}, {0x80,0x00},
  108. };
  109. static const u_char lintomulaw[256] = {
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
  111. 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03,
  112. 0x03, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05,
  113. 0x05, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07,
  114. 0x07, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09,
  115. 0x09, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0b, 0x0b,
  116. 0x0b, 0x0c, 0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d,
  117. 0x0d, 0x0e, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f,
  118. 0x0f, 0x10, 0x10, 0x11, 0x11, 0x12, 0x12, 0x13,
  119. 0x13, 0x14, 0x14, 0x15, 0x15, 0x16, 0x16, 0x17,
  120. 0x17, 0x18, 0x18, 0x19, 0x19, 0x1a, 0x1a, 0x1b,
  121. 0x1b, 0x1c, 0x1c, 0x1d, 0x1d, 0x1e, 0x1e, 0x1f,
  122. 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
  123. 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
  124. 0x2f, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c,
  125. 0x3e, 0x41, 0x45, 0x49, 0x4d, 0x53, 0x5b, 0x67,
  126. 0xff, 0xe7, 0xdb, 0xd3, 0xcd, 0xc9, 0xc5, 0xc1,
  127. 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0,
  128. 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8,
  129. 0xa7, 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1, 0xa0,
  130. 0x9f, 0x9f, 0x9e, 0x9e, 0x9d, 0x9d, 0x9c, 0x9c,
  131. 0x9b, 0x9b, 0x9a, 0x9a, 0x99, 0x99, 0x98, 0x98,
  132. 0x97, 0x97, 0x96, 0x96, 0x95, 0x95, 0x94, 0x94,
  133. 0x93, 0x93, 0x92, 0x92, 0x91, 0x91, 0x90, 0x90,
  134. 0x8f, 0x8f, 0x8f, 0x8f, 0x8e, 0x8e, 0x8e, 0x8e,
  135. 0x8d, 0x8d, 0x8d, 0x8d, 0x8c, 0x8c, 0x8c, 0x8c,
  136. 0x8b, 0x8b, 0x8b, 0x8b, 0x8a, 0x8a, 0x8a, 0x8a,
  137. 0x89, 0x89, 0x89, 0x89, 0x88, 0x88, 0x88, 0x88,
  138. 0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86,
  139. 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84,
  140. 0x83, 0x83, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82,
  141. 0x81, 0x81, 0x81, 0x81, 0x80, 0x80, 0x80, 0x80,
  142. };
  143. void
  144. mulaw_to_slinear8(u_char *p, int cc)
  145. {
  146. /* Use the 16 bit table for 8 bits too. */
  147. while (--cc >= 0) {
  148. *p = mulawtolin16[*p][0] ^ 0x80;
  149. ++p;
  150. }
  151. }
  152. void
  153. slinear8_to_mulaw(u_char *p, int cc)
  154. {
  155. while (--cc >= 0) {
  156. *p = lintomulaw[*p ^ 0x80];
  157. ++p;
  158. }
  159. }
  160. void
  161. mulaw24_to_slinear24(u_char *p, int cc)
  162. {
  163. int s, *q = (int *)p;
  164. while ((cc -= 4) >= 0) {
  165. s = (*q >> 16) & 0xff;
  166. *q = (int)((signed char)mulawtolin16[s][0] ^ 0x80) << 16 |
  167. (u_int)mulawtolin16[s][1] << 8;
  168. q++;
  169. }
  170. }
  171. void
  172. slinear24_to_mulaw24(u_char *p, int cc)
  173. {
  174. u_int *q = (u_int *)p;
  175. while ((cc -= 4) >= 0) {
  176. *q = lintomulaw[((*q >> 16) & 0xff) ^ 0x80] << 16;
  177. q++;
  178. }
  179. }