l1oip_codec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * l1oip_codec.c generic codec using lookup table
  3. * -> conversion from a-Law to u-Law
  4. * -> conversion from u-Law to a-Law
  5. * -> compression by reducing the number of sample resolution to 4
  6. *
  7. * NOTE: It is not compatible with any standard codec like ADPCM.
  8. *
  9. * Author Andreas Eversberg (jolly@eversberg.eu)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. How the codec works:
  27. --------------------
  28. The volume is increased to increase the dynamic range of the audio signal.
  29. Each sample is converted to a-LAW with only 16 steps of level resolution.
  30. A pair of two samples are stored in one byte.
  31. The first byte is stored in the upper bits, the second byte is stored in the
  32. lower bits.
  33. To speed up compression and decompression, two lookup tables are formed:
  34. - 16 bits index for two samples (law encoded) with 8 bit compressed result.
  35. - 8 bits index for one compressed data with 16 bits decompressed result.
  36. NOTE: The bytes are handled as they are law-encoded.
  37. */
  38. #include <linux/vmalloc.h>
  39. #include <linux/mISDNif.h>
  40. #include <linux/in.h>
  41. #include "core.h"
  42. #include "l1oip.h"
  43. /* definitions of codec. don't use calculations, code may run slower. */
  44. static u8 *table_com;
  45. static u16 *table_dec;
  46. /* alaw -> ulaw */
  47. static u8 alaw_to_ulaw[256] =
  48. {
  49. 0xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49,
  50. 0xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57,
  51. 0xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41,
  52. 0xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f,
  53. 0xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d,
  54. 0xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b,
  55. 0xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45,
  56. 0xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53,
  57. 0xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47,
  58. 0xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55,
  59. 0xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f,
  60. 0xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e,
  61. 0xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b,
  62. 0xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59,
  63. 0xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43,
  64. 0xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51,
  65. 0xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a,
  66. 0xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58,
  67. 0xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42,
  68. 0xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50,
  69. 0xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e,
  70. 0xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c,
  71. 0xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46,
  72. 0xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54,
  73. 0xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48,
  74. 0xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56,
  75. 0xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40,
  76. 0xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f,
  77. 0xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c,
  78. 0xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a,
  79. 0xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44,
  80. 0xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x52
  81. };
  82. /* ulaw -> alaw */
  83. static u8 ulaw_to_alaw[256] =
  84. {
  85. 0xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35,
  86. 0xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25,
  87. 0xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d,
  88. 0xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d,
  89. 0xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31,
  90. 0xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21,
  91. 0x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9,
  92. 0x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9,
  93. 0xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47,
  94. 0xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf,
  95. 0x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f,
  96. 0x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33,
  97. 0xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23,
  98. 0xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b,
  99. 0xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b,
  100. 0x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b,
  101. 0xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34,
  102. 0xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24,
  103. 0xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c,
  104. 0xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c,
  105. 0xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30,
  106. 0xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20,
  107. 0x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8,
  108. 0x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8,
  109. 0xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46,
  110. 0xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde,
  111. 0x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e,
  112. 0x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32,
  113. 0xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22,
  114. 0xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a,
  115. 0xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a,
  116. 0x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a
  117. };
  118. /* alaw -> 4bit compression */
  119. static u8 alaw_to_4bit[256] = {
  120. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  121. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  122. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  123. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  124. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  125. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  126. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  127. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  128. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  129. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  130. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0d, 0x02,
  131. 0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  132. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  133. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  134. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  135. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  136. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  137. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  138. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  139. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  140. 0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  141. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x01, 0x0a, 0x05,
  142. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  143. 0x0d, 0x02, 0x09, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  144. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  145. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  146. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  147. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  148. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  149. 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
  150. 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
  151. 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
  152. };
  153. /* 4bit -> alaw decompression */
  154. static u8 _4bit_to_alaw[16] = {
  155. 0x5d, 0x51, 0xd9, 0xd7, 0x5f, 0x53, 0xa3, 0x4b,
  156. 0x2a, 0x3a, 0x22, 0x2e, 0x26, 0x56, 0x20, 0x2c,
  157. };
  158. /* ulaw -> 4bit compression */
  159. static u8 ulaw_to_4bit[256] = {
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  165. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  166. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  167. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  168. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  169. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04,
  170. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  171. 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
  172. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  173. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  174. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  175. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08,
  176. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  177. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  178. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  179. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  180. 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  181. 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  182. 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
  183. 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
  184. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  185. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b,
  186. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  187. 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a,
  188. 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  189. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  190. 0x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  191. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  192. };
  193. /* 4bit -> ulaw decompression */
  194. static u8 _4bit_to_ulaw[16] = {
  195. 0x11, 0x21, 0x31, 0x40, 0x4e, 0x5c, 0x68, 0x71,
  196. 0xfe, 0xef, 0xe7, 0xdb, 0xcd, 0xbf, 0xaf, 0x9f,
  197. };
  198. /*
  199. * Compresses data to the result buffer
  200. * The result size must be at least half of the input buffer.
  201. * The number of samples also must be even!
  202. */
  203. int
  204. l1oip_law_to_4bit(u8 *data, int len, u8 *result, u32 *state)
  205. {
  206. int ii, i = 0, o = 0;
  207. if (!len)
  208. return 0;
  209. /* send saved byte and first input byte */
  210. if (*state) {
  211. *result++ = table_com[(((*state) << 8) & 0xff00) | (*data++)];
  212. len--;
  213. o++;
  214. }
  215. ii = len >> 1;
  216. while (i < ii) {
  217. *result++ = table_com[(data[0]<<8) | (data[1])];
  218. data += 2;
  219. i++;
  220. o++;
  221. }
  222. /* if len has an odd number, we save byte for next call */
  223. if (len & 1)
  224. *state = 0x100 + *data;
  225. else
  226. *state = 0;
  227. return o;
  228. }
  229. /* Decompress data to the result buffer
  230. * The result size must be the number of sample in packet. (2 * input data)
  231. * The number of samples in the result are even!
  232. */
  233. int
  234. l1oip_4bit_to_law(u8 *data, int len, u8 *result)
  235. {
  236. int i = 0;
  237. u16 r;
  238. while (i < len) {
  239. r = table_dec[*data++];
  240. *result++ = r >> 8;
  241. *result++ = r;
  242. i++;
  243. }
  244. return len << 1;
  245. }
  246. /*
  247. * law conversion
  248. */
  249. int
  250. l1oip_alaw_to_ulaw(u8 *data, int len, u8 *result)
  251. {
  252. int i = 0;
  253. while (i < len) {
  254. *result++ = alaw_to_ulaw[*data++];
  255. i++;
  256. }
  257. return len;
  258. }
  259. int
  260. l1oip_ulaw_to_alaw(u8 *data, int len, u8 *result)
  261. {
  262. int i = 0;
  263. while (i < len) {
  264. *result++ = ulaw_to_alaw[*data++];
  265. i++;
  266. }
  267. return len;
  268. }
  269. /*
  270. * generate/free compression and decompression table
  271. */
  272. void
  273. l1oip_4bit_free(void)
  274. {
  275. vfree(table_dec);
  276. vfree(table_com);
  277. table_com = NULL;
  278. table_dec = NULL;
  279. }
  280. int
  281. l1oip_4bit_alloc(int ulaw)
  282. {
  283. int i1, i2, c, sample;
  284. /* in case, it is called again */
  285. if (table_dec)
  286. return 0;
  287. /* alloc conversion tables */
  288. table_com = vzalloc(65536);
  289. table_dec = vzalloc(512);
  290. if (!table_com || !table_dec) {
  291. l1oip_4bit_free();
  292. return -ENOMEM;
  293. }
  294. /* generate compression table */
  295. i1 = 0;
  296. while (i1 < 256) {
  297. if (ulaw)
  298. c = ulaw_to_4bit[i1];
  299. else
  300. c = alaw_to_4bit[i1];
  301. i2 = 0;
  302. while (i2 < 256) {
  303. table_com[(i1 << 8) | i2] |= (c << 4);
  304. table_com[(i2 << 8) | i1] |= c;
  305. i2++;
  306. }
  307. i1++;
  308. }
  309. /* generate decompression table */
  310. i1 = 0;
  311. while (i1 < 16) {
  312. if (ulaw)
  313. sample = _4bit_to_ulaw[i1];
  314. else
  315. sample = _4bit_to_alaw[i1];
  316. i2 = 0;
  317. while (i2 < 16) {
  318. table_dec[(i1 << 4) | i2] |= (sample << 8);
  319. table_dec[(i2 << 4) | i1] |= sample;
  320. i2++;
  321. }
  322. i1++;
  323. }
  324. return 0;
  325. }