codec_speex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Translate between signed linear and Speex (Open Codec)
  22. *
  23. * http://www.speex.org
  24. * \note This work was motivated by Jeremy McNamara
  25. * hacked to be configurable by anthm and bkw 9/28/2004
  26. * \ingroup codecs
  27. */
  28. /*** MODULEINFO
  29. <depend>speex</depend>
  30. <depend>speex_preprocess</depend>
  31. <use>speexdsp</use>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include <fcntl.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <netinet/in.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <speex/speex.h>
  42. /* We require a post 1.1.8 version of Speex to enable preprocessing
  43. and better type handling */
  44. #ifdef _SPEEX_TYPES_H
  45. #include <speex/speex_preprocess.h>
  46. #endif
  47. #include "asterisk/lock.h"
  48. #include "asterisk/translate.h"
  49. #include "asterisk/module.h"
  50. #include "asterisk/config.h"
  51. #include "asterisk/options.h"
  52. #include "asterisk/logger.h"
  53. #include "asterisk/channel.h"
  54. #include "asterisk/utils.h"
  55. /* Sample frame data */
  56. #include "slin_speex_ex.h"
  57. #include "speex_slin_ex.h"
  58. /* codec variables */
  59. static int quality = 3;
  60. static int complexity = 2;
  61. static int enhancement = 0;
  62. static int vad = 0;
  63. static int vbr = 0;
  64. static float vbr_quality = 4;
  65. static int abr = 0;
  66. static int dtx = 0; /* set to 1 to enable silence detection */
  67. static int preproc = 0;
  68. static int pp_vad = 0;
  69. static int pp_agc = 0;
  70. static float pp_agc_level = 8000; /* XXX what is this 8000 ? */
  71. static int pp_denoise = 0;
  72. static int pp_dereverb = 0;
  73. static float pp_dereverb_decay = 0.4;
  74. static float pp_dereverb_level = 0.3;
  75. #define TYPE_SILENCE 0x2
  76. #define TYPE_HIGH 0x0
  77. #define TYPE_LOW 0x1
  78. #define TYPE_MASK 0x3
  79. #define BUFFER_SAMPLES 8000
  80. #define SPEEX_SAMPLES 160
  81. struct speex_coder_pvt {
  82. void *speex;
  83. SpeexBits bits;
  84. int framesize;
  85. int silent_state;
  86. #ifdef _SPEEX_TYPES_H
  87. SpeexPreprocessState *pp;
  88. spx_int16_t buf[BUFFER_SAMPLES];
  89. #else
  90. int16_t buf[BUFFER_SAMPLES]; /* input, waiting to be compressed */
  91. #endif
  92. };
  93. static int lintospeex_new(struct ast_trans_pvt *pvt)
  94. {
  95. struct speex_coder_pvt *tmp = pvt->pvt;
  96. if (!(tmp->speex = speex_encoder_init(&speex_nb_mode)))
  97. return -1;
  98. speex_bits_init(&tmp->bits);
  99. speex_bits_reset(&tmp->bits);
  100. speex_encoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
  101. speex_encoder_ctl(tmp->speex, SPEEX_SET_COMPLEXITY, &complexity);
  102. #ifdef _SPEEX_TYPES_H
  103. if (preproc) {
  104. tmp->pp = speex_preprocess_state_init(tmp->framesize, 8000); /* XXX what is this 8000 ? */
  105. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_VAD, &pp_vad);
  106. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC, &pp_agc);
  107. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &pp_agc_level);
  108. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DENOISE, &pp_denoise);
  109. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB, &pp_dereverb);
  110. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &pp_dereverb_decay);
  111. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &pp_dereverb_level);
  112. }
  113. #endif
  114. if (!abr && !vbr) {
  115. speex_encoder_ctl(tmp->speex, SPEEX_SET_QUALITY, &quality);
  116. if (vad)
  117. speex_encoder_ctl(tmp->speex, SPEEX_SET_VAD, &vad);
  118. }
  119. if (vbr) {
  120. speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR, &vbr);
  121. speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR_QUALITY, &vbr_quality);
  122. }
  123. if (abr)
  124. speex_encoder_ctl(tmp->speex, SPEEX_SET_ABR, &abr);
  125. if (dtx)
  126. speex_encoder_ctl(tmp->speex, SPEEX_SET_DTX, &dtx);
  127. tmp->silent_state = 0;
  128. return 0;
  129. }
  130. static int speextolin_new(struct ast_trans_pvt *pvt)
  131. {
  132. struct speex_coder_pvt *tmp = pvt->pvt;
  133. if (!(tmp->speex = speex_decoder_init(&speex_nb_mode)))
  134. return -1;
  135. speex_bits_init(&tmp->bits);
  136. speex_decoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
  137. if (enhancement)
  138. speex_decoder_ctl(tmp->speex, SPEEX_SET_ENH, &enhancement);
  139. return 0;
  140. }
  141. static struct ast_frame *lintospeex_sample(void)
  142. {
  143. static struct ast_frame f;
  144. f.frametype = AST_FRAME_VOICE;
  145. f.subclass = AST_FORMAT_SLINEAR;
  146. f.datalen = sizeof(slin_speex_ex);
  147. /* Assume 8000 Hz */
  148. f.samples = sizeof(slin_speex_ex)/2;
  149. f.mallocd = 0;
  150. f.offset = 0;
  151. f.src = __PRETTY_FUNCTION__;
  152. f.data = slin_speex_ex;
  153. return &f;
  154. }
  155. static struct ast_frame *speextolin_sample(void)
  156. {
  157. static struct ast_frame f;
  158. f.frametype = AST_FRAME_VOICE;
  159. f.subclass = AST_FORMAT_SPEEX;
  160. f.datalen = sizeof(speex_slin_ex);
  161. /* All frames are 20 ms long */
  162. f.samples = SPEEX_SAMPLES;
  163. f.mallocd = 0;
  164. f.offset = 0;
  165. f.src = __PRETTY_FUNCTION__;
  166. f.data = speex_slin_ex;
  167. return &f;
  168. }
  169. /*! \brief convert and store into outbuf */
  170. static int speextolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  171. {
  172. struct speex_coder_pvt *tmp = pvt->pvt;
  173. /* Assuming there's space left, decode into the current buffer at
  174. the tail location. Read in as many frames as there are */
  175. int x;
  176. int res;
  177. int16_t *dst = (int16_t *)pvt->outbuf;
  178. /* XXX fout is a temporary buffer, may have different types */
  179. #ifdef _SPEEX_TYPES_H
  180. spx_int16_t fout[1024];
  181. #else
  182. float fout[1024];
  183. #endif
  184. if (f->datalen == 0) { /* Native PLC interpolation */
  185. if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) {
  186. ast_log(LOG_WARNING, "Out of buffer space\n");
  187. return -1;
  188. }
  189. #ifdef _SPEEX_TYPES_H
  190. speex_decode_int(tmp->speex, NULL, dst + pvt->samples);
  191. #else
  192. speex_decode(tmp->speex, NULL, fout);
  193. for (x=0;x<tmp->framesize;x++) {
  194. dst[pvt->samples + x] = (int16_t)fout[x];
  195. }
  196. #endif
  197. pvt->samples += tmp->framesize;
  198. pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */
  199. return 0;
  200. }
  201. /* Read in bits */
  202. speex_bits_read_from(&tmp->bits, f->data, f->datalen);
  203. for (;;) {
  204. #ifdef _SPEEX_TYPES_H
  205. res = speex_decode_int(tmp->speex, &tmp->bits, fout);
  206. #else
  207. res = speex_decode(tmp->speex, &tmp->bits, fout);
  208. #endif
  209. if (res < 0)
  210. break;
  211. if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) {
  212. ast_log(LOG_WARNING, "Out of buffer space\n");
  213. return -1;
  214. }
  215. for (x = 0 ; x < tmp->framesize; x++)
  216. dst[pvt->samples + x] = (int16_t)fout[x];
  217. pvt->samples += tmp->framesize;
  218. pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */
  219. }
  220. return 0;
  221. }
  222. /*! \brief store input frame in work buffer */
  223. static int lintospeex_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  224. {
  225. struct speex_coder_pvt *tmp = pvt->pvt;
  226. /* XXX We should look at how old the rest of our stream is, and if it
  227. is too old, then we should overwrite it entirely, otherwise we can
  228. get artifacts of earlier talk that do not belong */
  229. memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
  230. pvt->samples += f->samples;
  231. return 0;
  232. }
  233. /*! \brief convert work buffer and produce output frame */
  234. static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt)
  235. {
  236. struct speex_coder_pvt *tmp = pvt->pvt;
  237. int is_speech=1;
  238. int datalen = 0; /* output bytes */
  239. int samples = 0; /* output samples */
  240. /* We can't work on anything less than a frame in size */
  241. if (pvt->samples < tmp->framesize)
  242. return NULL;
  243. speex_bits_reset(&tmp->bits);
  244. while (pvt->samples >= tmp->framesize) {
  245. #ifdef _SPEEX_TYPES_H
  246. /* Preprocess audio */
  247. if (preproc)
  248. is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL);
  249. /* Encode a frame of data */
  250. if (is_speech) {
  251. /* If DTX enabled speex_encode returns 0 during silence */
  252. is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx;
  253. } else {
  254. /* 5 zeros interpreted by Speex as silence (submode 0) */
  255. speex_bits_pack(&tmp->bits, 0, 5);
  256. }
  257. #else
  258. {
  259. float fbuf[1024];
  260. int x;
  261. /* Convert to floating point */
  262. for (x = 0; x < tmp->framesize; x++)
  263. fbuf[x] = tmp->buf[samples + x];
  264. /* Encode a frame of data */
  265. is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx;
  266. }
  267. #endif
  268. samples += tmp->framesize;
  269. pvt->samples -= tmp->framesize;
  270. }
  271. /* Move the data at the end of the buffer to the front */
  272. if (pvt->samples)
  273. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  274. /* Use AST_FRAME_CNG to signify the start of any silence period */
  275. if (is_speech) {
  276. tmp->silent_state = 0;
  277. } else {
  278. if (tmp->silent_state) {
  279. return NULL;
  280. } else {
  281. tmp->silent_state = 1;
  282. speex_bits_reset(&tmp->bits);
  283. memset(&pvt->f, 0, sizeof(pvt->f));
  284. pvt->f.frametype = AST_FRAME_CNG;
  285. pvt->f.samples = samples;
  286. /* XXX what now ? format etc... */
  287. }
  288. }
  289. /* Terminate bit stream */
  290. speex_bits_pack(&tmp->bits, 15, 5);
  291. datalen = speex_bits_write(&tmp->bits, pvt->outbuf, pvt->t->buf_size);
  292. return ast_trans_frameout(pvt, datalen, samples);
  293. }
  294. static void speextolin_destroy(struct ast_trans_pvt *arg)
  295. {
  296. struct speex_coder_pvt *pvt = arg->pvt;
  297. speex_decoder_destroy(pvt->speex);
  298. speex_bits_destroy(&pvt->bits);
  299. }
  300. static void lintospeex_destroy(struct ast_trans_pvt *arg)
  301. {
  302. struct speex_coder_pvt *pvt = arg->pvt;
  303. #ifdef _SPEEX_TYPES_H
  304. if (preproc)
  305. speex_preprocess_state_destroy(pvt->pp);
  306. #endif
  307. speex_encoder_destroy(pvt->speex);
  308. speex_bits_destroy(&pvt->bits);
  309. }
  310. static struct ast_translator speextolin = {
  311. .name = "speextolin",
  312. .srcfmt = AST_FORMAT_SPEEX,
  313. .dstfmt = AST_FORMAT_SLINEAR,
  314. .newpvt = speextolin_new,
  315. .framein = speextolin_framein,
  316. .destroy = speextolin_destroy,
  317. .sample = speextolin_sample,
  318. .desc_size = sizeof(struct speex_coder_pvt),
  319. .buffer_samples = BUFFER_SAMPLES,
  320. .buf_size = BUFFER_SAMPLES * 2,
  321. .native_plc = 1,
  322. };
  323. static struct ast_translator lintospeex = {
  324. .name = "lintospeex",
  325. .srcfmt = AST_FORMAT_SLINEAR,
  326. .dstfmt = AST_FORMAT_SPEEX,
  327. .newpvt = lintospeex_new,
  328. .framein = lintospeex_framein,
  329. .frameout = lintospeex_frameout,
  330. .destroy = lintospeex_destroy,
  331. .sample = lintospeex_sample,
  332. .desc_size = sizeof(struct speex_coder_pvt),
  333. .buffer_samples = BUFFER_SAMPLES,
  334. .buf_size = BUFFER_SAMPLES * 2, /* XXX maybe a lot less ? */
  335. };
  336. static void parse_config(void)
  337. {
  338. struct ast_config *cfg = ast_config_load("codecs.conf");
  339. struct ast_variable *var;
  340. int res;
  341. float res_f;
  342. if (cfg == NULL)
  343. return;
  344. for (var = ast_variable_browse(cfg, "speex"); var; var = var->next) {
  345. if (!strcasecmp(var->name, "quality")) {
  346. res = abs(atoi(var->value));
  347. if (res > -1 && res < 11) {
  348. if (option_verbose > 2)
  349. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting Quality to %d\n",res);
  350. quality = res;
  351. } else
  352. ast_log(LOG_ERROR,"Error Quality must be 0-10\n");
  353. } else if (!strcasecmp(var->name, "complexity")) {
  354. res = abs(atoi(var->value));
  355. if (res > -1 && res < 11) {
  356. if (option_verbose > 2)
  357. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting Complexity to %d\n",res);
  358. complexity = res;
  359. } else
  360. ast_log(LOG_ERROR,"Error! Complexity must be 0-10\n");
  361. } else if (!strcasecmp(var->name, "vbr_quality")) {
  362. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0 && res_f <= 10) {
  363. if (option_verbose > 2)
  364. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting VBR Quality to %f\n",res_f);
  365. vbr_quality = res_f;
  366. } else
  367. ast_log(LOG_ERROR,"Error! VBR Quality must be 0-10\n");
  368. } else if (!strcasecmp(var->name, "abr_quality")) {
  369. ast_log(LOG_ERROR,"Error! ABR Quality setting obsolete, set ABR to desired bitrate\n");
  370. } else if (!strcasecmp(var->name, "enhancement")) {
  371. enhancement = ast_true(var->value) ? 1 : 0;
  372. if (option_verbose > 2)
  373. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ? "on" : "off");
  374. } else if (!strcasecmp(var->name, "vbr")) {
  375. vbr = ast_true(var->value) ? 1 : 0;
  376. if (option_verbose > 2)
  377. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: VBR Mode. [%s]\n",vbr ? "on" : "off");
  378. } else if (!strcasecmp(var->name, "abr")) {
  379. res = abs(atoi(var->value));
  380. if (res >= 0) {
  381. if (option_verbose > 2) {
  382. if (res > 0)
  383. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting ABR target bitrate to %d\n",res);
  384. else
  385. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Disabling ABR\n");
  386. }
  387. abr = res;
  388. } else
  389. ast_log(LOG_ERROR,"Error! ABR target bitrate must be >= 0\n");
  390. } else if (!strcasecmp(var->name, "vad")) {
  391. vad = ast_true(var->value) ? 1 : 0;
  392. if (option_verbose > 2)
  393. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: VAD Mode. [%s]\n",vad ? "on" : "off");
  394. } else if (!strcasecmp(var->name, "dtx")) {
  395. dtx = ast_true(var->value) ? 1 : 0;
  396. if (option_verbose > 2)
  397. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: DTX Mode. [%s]\n",dtx ? "on" : "off");
  398. } else if (!strcasecmp(var->name, "preprocess")) {
  399. preproc = ast_true(var->value) ? 1 : 0;
  400. if (option_verbose > 2)
  401. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessing. [%s]\n",preproc ? "on" : "off");
  402. } else if (!strcasecmp(var->name, "pp_vad")) {
  403. pp_vad = ast_true(var->value) ? 1 : 0;
  404. if (option_verbose > 2)
  405. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor VAD. [%s]\n",pp_vad ? "on" : "off");
  406. } else if (!strcasecmp(var->name, "pp_agc")) {
  407. pp_agc = ast_true(var->value) ? 1 : 0;
  408. if (option_verbose > 2)
  409. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ? "on" : "off");
  410. } else if (!strcasecmp(var->name, "pp_agc_level")) {
  411. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  412. if (option_verbose > 2)
  413. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor AGC Level to %f\n",res_f);
  414. pp_agc_level = res_f;
  415. } else
  416. ast_log(LOG_ERROR,"Error! Preprocessor AGC Level must be >= 0\n");
  417. } else if (!strcasecmp(var->name, "pp_denoise")) {
  418. pp_denoise = ast_true(var->value) ? 1 : 0;
  419. if (option_verbose > 2)
  420. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor Denoise. [%s]\n",pp_denoise ? "on" : "off");
  421. } else if (!strcasecmp(var->name, "pp_dereverb")) {
  422. pp_dereverb = ast_true(var->value) ? 1 : 0;
  423. if (option_verbose > 2)
  424. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ? "on" : "off");
  425. } else if (!strcasecmp(var->name, "pp_dereverb_decay")) {
  426. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  427. if (option_verbose > 2)
  428. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor Dereverb Decay to %f\n",res_f);
  429. pp_dereverb_decay = res_f;
  430. } else
  431. ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Decay must be >= 0\n");
  432. } else if (!strcasecmp(var->name, "pp_dereverb_level")) {
  433. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  434. if (option_verbose > 2)
  435. ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor Dereverb Level to %f\n",res_f);
  436. pp_dereverb_level = res_f;
  437. } else
  438. ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Level must be >= 0\n");
  439. }
  440. }
  441. ast_config_destroy(cfg);
  442. }
  443. static int reload(void)
  444. {
  445. parse_config();
  446. return 0;
  447. }
  448. static int unload_module(void)
  449. {
  450. int res;
  451. res = ast_unregister_translator(&lintospeex);
  452. res |= ast_unregister_translator(&speextolin);
  453. return res;
  454. }
  455. static int load_module(void)
  456. {
  457. int res;
  458. parse_config();
  459. res=ast_register_translator(&speextolin);
  460. if (!res)
  461. res=ast_register_translator(&lintospeex);
  462. else
  463. ast_unregister_translator(&speextolin);
  464. return res;
  465. }
  466. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Speex Coder/Decoder",
  467. .load = load_module,
  468. .unload = unload_module,
  469. .reload = reload,
  470. );