codec_g723_1.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Translate between signed linear and G.723.1
  5. *
  6. * The G.723.1 code is not included in the Asterisk distribution because
  7. * it is covered with patents, and in spite of statements to the contrary,
  8. * the "technology" is extremely expensive to license.
  9. *
  10. * Copyright (C) 1999, Mark Spencer
  11. *
  12. * Mark Spencer <markster@linux-support.net>
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License
  16. */
  17. #define TYPE_HIGH 0x0
  18. #define TYPE_LOW 0x1
  19. #define TYPE_SILENCE 0x2
  20. #define TYPE_DONTSEND 0x3
  21. #define TYPE_MASK 0x3
  22. #include <sys/types.h>
  23. #include <asterisk/lock.h>
  24. #include <asterisk/translate.h>
  25. #include <asterisk/module.h>
  26. #include <asterisk/logger.h>
  27. #include <asterisk/channel.h>
  28. #include <fcntl.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <netinet/in.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #ifdef ANNEX_B
  35. #include "g723.1b/typedef2.h"
  36. #include "g723.1b/cst2.h"
  37. #include "g723.1b/coder2.h"
  38. #include "g723.1b/decod2.h"
  39. #include "g723.1b/deccng2.h"
  40. #include "g723.1b/codcng2.h"
  41. #include "g723.1b/vad2.h"
  42. #else
  43. #include "g723.1/typedef.h"
  44. #include "g723.1/cst_lbc.h"
  45. #include "g723.1/coder.h"
  46. #include "g723.1/decod.h"
  47. #include "g723.1/dec_cng.h"
  48. #include "g723.1/cod_cng.h"
  49. #include "g723.1/vad.h"
  50. #endif
  51. /* Sample frame data */
  52. #include "slin_g723_ex.h"
  53. #include "g723_slin_ex.h"
  54. AST_MUTEX_DEFINE_STATIC(localuser_lock);
  55. static int localusecnt=0;
  56. #ifdef ANNEX_B
  57. static char *tdesc = "Annex B (floating point) G.723.1/PCM16 Codec Translator";
  58. #else
  59. static char *tdesc = "Annex A (fixed point) G.723.1/PCM16 Codec Translator";
  60. #endif
  61. /* Globals */
  62. Flag UsePf = True;
  63. Flag UseHp = True;
  64. Flag UseVx = True;
  65. enum Crate WrkRate = Rate63;
  66. struct g723_encoder_pvt {
  67. struct cod_state cod;
  68. struct ast_frame f;
  69. /* Space to build offset */
  70. char offset[AST_FRIENDLY_OFFSET];
  71. /* Buffer for our outgoing frame */
  72. char outbuf[8000];
  73. /* Enough to store a full second */
  74. short buf[8000];
  75. int tail;
  76. };
  77. struct g723_decoder_pvt {
  78. struct dec_state dec;
  79. struct ast_frame f;
  80. /* Space to build offset */
  81. char offset[AST_FRIENDLY_OFFSET];
  82. /* Enough to store a full second */
  83. short buf[8000];
  84. int tail;
  85. };
  86. static struct ast_translator_pvt *g723tolin_new(void)
  87. {
  88. struct g723_decoder_pvt *tmp;
  89. tmp = malloc(sizeof(struct g723_decoder_pvt));
  90. if (tmp) {
  91. Init_Decod(&tmp->dec);
  92. Init_Dec_Cng(&tmp->dec);
  93. tmp->tail = 0;
  94. localusecnt++;
  95. ast_update_use_count();
  96. }
  97. return (struct ast_translator_pvt *)tmp;
  98. }
  99. static struct ast_frame *lintog723_sample(void)
  100. {
  101. static struct ast_frame f;
  102. f.frametype = AST_FRAME_VOICE;
  103. f.subclass = AST_FORMAT_SLINEAR;
  104. f.datalen = sizeof(slin_g723_ex);
  105. /* Assume 8000 Hz */
  106. f.samples = sizeof(slin_g723_ex)/2;
  107. f.mallocd = 0;
  108. f.offset = 0;
  109. f.src = __PRETTY_FUNCTION__;
  110. f.data = slin_g723_ex;
  111. return &f;
  112. }
  113. static struct ast_frame *g723tolin_sample(void)
  114. {
  115. static struct ast_frame f;
  116. f.frametype = AST_FRAME_VOICE;
  117. f.subclass = AST_FORMAT_G723_1;
  118. f.datalen = sizeof(g723_slin_ex);
  119. /* All frames are 30 ms long */
  120. f.samples = 240;
  121. f.mallocd = 0;
  122. f.offset = 0;
  123. f.src = __PRETTY_FUNCTION__;
  124. f.data = g723_slin_ex;
  125. return &f;
  126. }
  127. static struct ast_translator_pvt *lintog723_new(void)
  128. {
  129. struct g723_encoder_pvt *tmp;
  130. tmp = malloc(sizeof(struct g723_encoder_pvt));
  131. if (tmp) {
  132. Init_Coder(&tmp->cod);
  133. /* Init Comfort Noise Functions */
  134. if( UseVx ) {
  135. Init_Vad(&tmp->cod);
  136. Init_Cod_Cng(&tmp->cod);
  137. }
  138. localusecnt++;
  139. ast_update_use_count();
  140. tmp->tail = 0;
  141. }
  142. return (struct ast_translator_pvt *)tmp;
  143. }
  144. static struct ast_frame *g723tolin_frameout(struct ast_translator_pvt *pvt)
  145. {
  146. struct g723_decoder_pvt *tmp = (struct g723_decoder_pvt *)pvt;
  147. if (!tmp->tail)
  148. return NULL;
  149. /* Signed linear is no particular frame size, so just send whatever
  150. we have in the buffer in one lump sum */
  151. tmp->f.frametype = AST_FRAME_VOICE;
  152. tmp->f.subclass = AST_FORMAT_SLINEAR;
  153. tmp->f.datalen = tmp->tail * 2;
  154. /* Assume 8000 Hz */
  155. tmp->f.samples = tmp->tail;
  156. tmp->f.mallocd = 0;
  157. tmp->f.offset = AST_FRIENDLY_OFFSET;
  158. tmp->f.src = __PRETTY_FUNCTION__;
  159. tmp->f.data = tmp->buf;
  160. /* Reset tail pointer */
  161. tmp->tail = 0;
  162. #if 0
  163. /* Save the frames */
  164. {
  165. static int fd2 = -1;
  166. if (fd2 == -1) {
  167. fd2 = open("g723.example", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  168. }
  169. write(fd2, tmp->f.data, tmp->f.datalen);
  170. }
  171. #endif
  172. return &tmp->f;
  173. }
  174. static int g723_len(unsigned char buf)
  175. {
  176. switch(buf & TYPE_MASK) {
  177. case TYPE_DONTSEND:
  178. return 0;
  179. break;
  180. case TYPE_SILENCE:
  181. return 4;
  182. break;
  183. case TYPE_HIGH:
  184. return 24;
  185. break;
  186. case TYPE_LOW:
  187. return 20;
  188. break;
  189. default:
  190. ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
  191. }
  192. return -1;
  193. }
  194. static int g723tolin_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
  195. {
  196. struct g723_decoder_pvt *tmp = (struct g723_decoder_pvt *)pvt;
  197. int len = 0;
  198. int res;
  199. #ifdef ANNEX_B
  200. FLOAT tmpdata[Frame];
  201. int x;
  202. #endif
  203. while(len < f->datalen) {
  204. /* Assuming there's space left, decode into the current buffer at
  205. the tail location */
  206. res = g723_len(((unsigned char *)f->data + len)[0]);
  207. if (res < 0) {
  208. ast_log(LOG_WARNING, "Invalid data\n");
  209. return -1;
  210. }
  211. if (res + len > f->datalen) {
  212. ast_log(LOG_WARNING, "Measured length exceeds frame length\n");
  213. return -1;
  214. }
  215. if (tmp->tail + Frame < sizeof(tmp->buf)/2) {
  216. #ifdef ANNEX_B
  217. Decod(&tmp->dec, tmpdata, f->data + len, 0);
  218. for (x=0;x<Frame;x++)
  219. (tmp->buf + tmp->tail)[x] = (short)(tmpdata[x]);
  220. #else
  221. Decod(&tmp->dec, tmp->buf + tmp->tail, f->data + len, 0);
  222. #endif
  223. tmp->tail+=Frame;
  224. } else {
  225. ast_log(LOG_WARNING, "Out of buffer space\n");
  226. return -1;
  227. }
  228. len += res;
  229. }
  230. return 0;
  231. }
  232. static int lintog723_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
  233. {
  234. /* Just add the frames to our stream */
  235. /* XXX We should look at how old the rest of our stream is, and if it
  236. is too old, then we should overwrite it entirely, otherwise we can
  237. get artifacts of earlier talk that do not belong */
  238. struct g723_encoder_pvt *tmp = (struct g723_encoder_pvt *)pvt;
  239. if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
  240. memcpy(&tmp->buf[tmp->tail], f->data, f->datalen);
  241. tmp->tail += f->datalen/2;
  242. } else {
  243. ast_log(LOG_WARNING, "Out of buffer space\n");
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. static struct ast_frame *lintog723_frameout(struct ast_translator_pvt *pvt)
  249. {
  250. struct g723_encoder_pvt *tmp = (struct g723_encoder_pvt *)pvt;
  251. #ifdef ANNEX_B
  252. int x;
  253. FLOAT tmpdata[Frame];
  254. #endif
  255. int cnt=0;
  256. /* We can't work on anything less than a frame in size */
  257. if (tmp->tail < Frame)
  258. return NULL;
  259. tmp->f.frametype = AST_FRAME_VOICE;
  260. tmp->f.subclass = AST_FORMAT_G723_1;
  261. tmp->f.offset = AST_FRIENDLY_OFFSET;
  262. tmp->f.src = __PRETTY_FUNCTION__;
  263. tmp->f.samples = 0;
  264. tmp->f.mallocd = 0;
  265. while(tmp->tail >= Frame) {
  266. /* Encode a frame of data */
  267. if (cnt + 24 >= sizeof(tmp->outbuf)) {
  268. ast_log(LOG_WARNING, "Out of buffer space\n");
  269. return NULL;
  270. }
  271. #ifdef ANNEX_B
  272. for (x=0;x<Frame;x++)
  273. tmpdata[x] = tmp->buf[x];
  274. Coder(&tmp->cod, tmpdata, tmp->outbuf + cnt);
  275. #else
  276. Coder(&tmp->cod, tmp->buf, tmp->outbuf + cnt);
  277. #endif
  278. /* Assume 8000 Hz */
  279. tmp->f.samples += 240;
  280. cnt += g723_len(tmp->outbuf[cnt]);
  281. tmp->tail -= Frame;
  282. /* Move the data at the end of the buffer to the front */
  283. if (tmp->tail)
  284. memmove(tmp->buf, tmp->buf + Frame, tmp->tail * 2);
  285. }
  286. tmp->f.datalen = cnt;
  287. tmp->f.data = tmp->outbuf;
  288. #if 0
  289. /* Save to a g723 sample output file... */
  290. {
  291. static int fd = -1;
  292. int delay = htonl(30);
  293. short size;
  294. if (fd < 0)
  295. fd = open("trans.g723", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  296. if (fd < 0)
  297. ast_log(LOG_WARNING, "Unable to create demo\n");
  298. write(fd, &delay, 4);
  299. size = htons(tmp->f.datalen);
  300. write(fd, &size, 2);
  301. write(fd, tmp->f.data, tmp->f.datalen);
  302. }
  303. #endif
  304. return &tmp->f;
  305. }
  306. static void g723_destroy(struct ast_translator_pvt *pvt)
  307. {
  308. free(pvt);
  309. localusecnt--;
  310. ast_update_use_count();
  311. }
  312. static struct ast_translator g723tolin =
  313. #ifdef ANNEX_B
  314. { "g723tolinb",
  315. #else
  316. { "g723tolin",
  317. #endif
  318. AST_FORMAT_G723_1, AST_FORMAT_SLINEAR,
  319. g723tolin_new,
  320. g723tolin_framein,
  321. g723tolin_frameout,
  322. g723_destroy,
  323. g723tolin_sample
  324. };
  325. static struct ast_translator lintog723 =
  326. #ifdef ANNEX_B
  327. { "lintog723b",
  328. #else
  329. { "lintog723",
  330. #endif
  331. AST_FORMAT_SLINEAR, AST_FORMAT_G723_1,
  332. lintog723_new,
  333. lintog723_framein,
  334. lintog723_frameout,
  335. g723_destroy,
  336. lintog723_sample
  337. };
  338. int unload_module(void)
  339. {
  340. int res;
  341. ast_mutex_lock(&localuser_lock);
  342. res = ast_unregister_translator(&lintog723);
  343. if (!res)
  344. res = ast_unregister_translator(&g723tolin);
  345. if (localusecnt)
  346. res = -1;
  347. ast_mutex_unlock(&localuser_lock);
  348. return res;
  349. }
  350. int load_module(void)
  351. {
  352. int res;
  353. res=ast_register_translator(&g723tolin);
  354. if (!res)
  355. res=ast_register_translator(&lintog723);
  356. else
  357. ast_unregister_translator(&g723tolin);
  358. return res;
  359. }
  360. char *description(void)
  361. {
  362. return tdesc;
  363. }
  364. int usecount(void)
  365. {
  366. int res;
  367. STANDARD_USECOUNT(res);
  368. return res;
  369. }
  370. char *key(void)
  371. {
  372. return ASTERISK_GPL_KEY;
  373. }