codec_ulaw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* codec_ulaw.c - translate between signed linear and ulaw
  2. *
  3. * Asterisk -- A telephony toolkit for Linux.
  4. *
  5. * Copyright (c) 2001 Linux Support Services, Inc. All rights reserved.
  6. *
  7. * Mark Spencer <markster@linux-support.net
  8. *
  9. * This program is free software, distributed under the terms of
  10. * the GNU General Public License
  11. */
  12. #include <asterisk/lock.h>
  13. #include <asterisk/logger.h>
  14. #include <asterisk/module.h>
  15. #include <asterisk/translate.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/ulaw.h>
  18. #include <fcntl.h>
  19. #include <netinet/in.h>
  20. #include <pthread.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #define BUFFER_SIZE 8096 /* size for the translation buffers */
  26. static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
  27. static int localusecnt = 0;
  28. static char *tdesc = "Mu-law Coder/Decoder";
  29. /* Sample frame data */
  30. #include "slin_ulaw_ex.h"
  31. #include "ulaw_slin_ex.h"
  32. /*
  33. * Private workspace for translating signed linear signals to ulaw.
  34. */
  35. struct ulaw_encoder_pvt
  36. {
  37. struct ast_frame f;
  38. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  39. unsigned char outbuf[BUFFER_SIZE]; /* Encoded ulaw, two nibbles to a word */
  40. int tail;
  41. };
  42. /*
  43. * Private workspace for translating ulaw signals to signed linear.
  44. */
  45. struct ulaw_decoder_pvt
  46. {
  47. struct ast_frame f;
  48. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  49. short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
  50. int tail;
  51. };
  52. /*
  53. * ulawToLin_New
  54. * Create a new instance of ulaw_decoder_pvt.
  55. *
  56. * Results:
  57. * Returns a pointer to the new instance.
  58. *
  59. * Side effects:
  60. * None.
  61. */
  62. static struct ast_translator_pvt *
  63. ulawtolin_new (void)
  64. {
  65. struct ulaw_decoder_pvt *tmp;
  66. tmp = malloc (sizeof (struct ulaw_decoder_pvt));
  67. if (tmp)
  68. {
  69. memset(tmp, 0, sizeof(*tmp));
  70. tmp->tail = 0;
  71. localusecnt++;
  72. ast_update_use_count ();
  73. }
  74. return (struct ast_translator_pvt *) tmp;
  75. }
  76. /*
  77. * LinToulaw_New
  78. * Create a new instance of ulaw_encoder_pvt.
  79. *
  80. * Results:
  81. * Returns a pointer to the new instance.
  82. *
  83. * Side effects:
  84. * None.
  85. */
  86. static struct ast_translator_pvt *
  87. lintoulaw_new (void)
  88. {
  89. struct ulaw_encoder_pvt *tmp;
  90. tmp = malloc (sizeof (struct ulaw_encoder_pvt));
  91. if (tmp)
  92. {
  93. memset(tmp, 0, sizeof(*tmp));
  94. localusecnt++;
  95. ast_update_use_count ();
  96. tmp->tail = 0;
  97. }
  98. return (struct ast_translator_pvt *) tmp;
  99. }
  100. /*
  101. * ulawToLin_FrameIn
  102. * Fill an input buffer with packed 4-bit ulaw values if there is room
  103. * left.
  104. *
  105. * Results:
  106. * Foo
  107. *
  108. * Side effects:
  109. * tmp->tail is the number of packed values in the buffer.
  110. */
  111. static int
  112. ulawtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  113. {
  114. struct ulaw_decoder_pvt *tmp = (struct ulaw_decoder_pvt *) pvt;
  115. int x;
  116. unsigned char *b;
  117. if ((tmp->tail + f->datalen) * 2 > sizeof(tmp->outbuf)) {
  118. ast_log(LOG_WARNING, "Out of buffer space\n");
  119. return -1;
  120. }
  121. /* Reset ssindex and signal to frame's specified values */
  122. b = f->data;
  123. for (x=0;x<f->datalen;x++)
  124. tmp->outbuf[tmp->tail + x] = AST_MULAW(b[x]);
  125. tmp->tail += f->datalen;
  126. return 0;
  127. }
  128. /*
  129. * ulawToLin_FrameOut
  130. * Convert 4-bit ulaw encoded signals to 16-bit signed linear.
  131. *
  132. * Results:
  133. * Converted signals are placed in tmp->f.data, tmp->f.datalen
  134. * and tmp->f.samples are calculated.
  135. *
  136. * Side effects:
  137. * None.
  138. */
  139. static struct ast_frame *
  140. ulawtolin_frameout (struct ast_translator_pvt *pvt)
  141. {
  142. struct ulaw_decoder_pvt *tmp = (struct ulaw_decoder_pvt *) pvt;
  143. if (!tmp->tail)
  144. return NULL;
  145. tmp->f.frametype = AST_FRAME_VOICE;
  146. tmp->f.subclass = AST_FORMAT_SLINEAR;
  147. tmp->f.datalen = tmp->tail *2;
  148. tmp->f.samples = tmp->tail;
  149. tmp->f.mallocd = 0;
  150. tmp->f.offset = AST_FRIENDLY_OFFSET;
  151. tmp->f.src = __PRETTY_FUNCTION__;
  152. tmp->f.data = tmp->outbuf;
  153. tmp->tail = 0;
  154. return &tmp->f;
  155. }
  156. /*
  157. * LinToulaw_FrameIn
  158. * Fill an input buffer with 16-bit signed linear PCM values.
  159. *
  160. * Results:
  161. * None.
  162. *
  163. * Side effects:
  164. * tmp->tail is number of signal values in the input buffer.
  165. */
  166. static int
  167. lintoulaw_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  168. {
  169. struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
  170. int x;
  171. short *s;
  172. if (tmp->tail + f->datalen/2 >= sizeof(tmp->outbuf))
  173. {
  174. ast_log (LOG_WARNING, "Out of buffer space\n");
  175. return -1;
  176. }
  177. s = f->data;
  178. for (x=0;x<f->datalen/2;x++)
  179. tmp->outbuf[x+tmp->tail] = AST_LIN2MU(s[x]);
  180. tmp->tail += f->datalen/2;
  181. return 0;
  182. }
  183. /*
  184. * LinToulaw_FrameOut
  185. * Convert a buffer of raw 16-bit signed linear PCM to a buffer
  186. * of 4-bit ulaw packed two to a byte (Big Endian).
  187. *
  188. * Results:
  189. * Foo
  190. *
  191. * Side effects:
  192. * Leftover inbuf data gets packed, tail gets updated.
  193. */
  194. static struct ast_frame *
  195. lintoulaw_frameout (struct ast_translator_pvt *pvt)
  196. {
  197. struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
  198. if (tmp->tail) {
  199. tmp->f.frametype = AST_FRAME_VOICE;
  200. tmp->f.subclass = AST_FORMAT_ULAW;
  201. tmp->f.samples = tmp->tail;
  202. tmp->f.mallocd = 0;
  203. tmp->f.offset = AST_FRIENDLY_OFFSET;
  204. tmp->f.src = __PRETTY_FUNCTION__;
  205. tmp->f.data = tmp->outbuf;
  206. tmp->f.datalen = tmp->tail;
  207. tmp->tail = 0;
  208. return &tmp->f;
  209. } else return NULL;
  210. }
  211. /*
  212. * ulawToLin_Sample
  213. */
  214. static struct ast_frame *
  215. ulawtolin_sample (void)
  216. {
  217. static struct ast_frame f;
  218. f.frametype = AST_FRAME_VOICE;
  219. f.subclass = AST_FORMAT_ULAW;
  220. f.datalen = sizeof (ulaw_slin_ex);
  221. f.samples = sizeof(ulaw_slin_ex);
  222. f.mallocd = 0;
  223. f.offset = 0;
  224. f.src = __PRETTY_FUNCTION__;
  225. f.data = ulaw_slin_ex;
  226. return &f;
  227. }
  228. /*
  229. * LinToulaw_Sample
  230. */
  231. static struct ast_frame *
  232. lintoulaw_sample (void)
  233. {
  234. static struct ast_frame f;
  235. f.frametype = AST_FRAME_VOICE;
  236. f.subclass = AST_FORMAT_SLINEAR;
  237. f.datalen = sizeof (slin_ulaw_ex);
  238. /* Assume 8000 Hz */
  239. f.samples = sizeof (slin_ulaw_ex) / 2;
  240. f.mallocd = 0;
  241. f.offset = 0;
  242. f.src = __PRETTY_FUNCTION__;
  243. f.data = slin_ulaw_ex;
  244. return &f;
  245. }
  246. /*
  247. * ulaw_Destroy
  248. * Destroys a private workspace.
  249. *
  250. * Results:
  251. * It's gone!
  252. *
  253. * Side effects:
  254. * None.
  255. */
  256. static void
  257. ulaw_destroy (struct ast_translator_pvt *pvt)
  258. {
  259. free (pvt);
  260. localusecnt--;
  261. ast_update_use_count ();
  262. }
  263. /*
  264. * The complete translator for ulawToLin.
  265. */
  266. static struct ast_translator ulawtolin = {
  267. "ulawtolin",
  268. AST_FORMAT_ULAW,
  269. AST_FORMAT_SLINEAR,
  270. ulawtolin_new,
  271. ulawtolin_framein,
  272. ulawtolin_frameout,
  273. ulaw_destroy,
  274. /* NULL */
  275. ulawtolin_sample
  276. };
  277. /*
  278. * The complete translator for LinToulaw.
  279. */
  280. static struct ast_translator lintoulaw = {
  281. "lintoulaw",
  282. AST_FORMAT_SLINEAR,
  283. AST_FORMAT_ULAW,
  284. lintoulaw_new,
  285. lintoulaw_framein,
  286. lintoulaw_frameout,
  287. ulaw_destroy,
  288. /* NULL */
  289. lintoulaw_sample
  290. };
  291. int
  292. unload_module (void)
  293. {
  294. int res;
  295. ast_mutex_lock (&localuser_lock);
  296. res = ast_unregister_translator (&lintoulaw);
  297. if (!res)
  298. res = ast_unregister_translator (&ulawtolin);
  299. if (localusecnt)
  300. res = -1;
  301. ast_mutex_unlock (&localuser_lock);
  302. return res;
  303. }
  304. int
  305. load_module (void)
  306. {
  307. int res;
  308. res = ast_register_translator (&ulawtolin);
  309. if (!res)
  310. res = ast_register_translator (&lintoulaw);
  311. else
  312. ast_unregister_translator (&ulawtolin);
  313. return res;
  314. }
  315. /*
  316. * Return a description of this module.
  317. */
  318. char *
  319. description (void)
  320. {
  321. return tdesc;
  322. }
  323. int
  324. usecount (void)
  325. {
  326. int res;
  327. STANDARD_USECOUNT (res);
  328. return res;
  329. }
  330. char *
  331. key ()
  332. {
  333. return ASTERISK_GPL_KEY;
  334. }