Translate between signed linear and Speex (Open Codec) More...
#include "asterisk.h"
#include <speex/speex.h>
#include "asterisk/translate.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/utils.h"
#include "asterisk/slin.h"
#include "ex_speex.h"
Go to the source code of this file.
Data Structures | |
struct | speex_coder_pvt |
Defines | |
#define | BUFFER_SAMPLES 8000 |
#define | SPEEX_SAMPLES 160 |
#define | TYPE_HIGH 0x0 |
#define | TYPE_LOW 0x1 |
#define | TYPE_MASK 0x3 |
#define | TYPE_SILENCE 0x2 |
Functions | |
static void | __reg_module (void) |
static void | __unreg_module (void) |
static void | lintospeex_destroy (struct ast_trans_pvt *arg) |
static int | lintospeex_framein (struct ast_trans_pvt *pvt, struct ast_frame *f) |
store input frame in work buffer | |
static struct ast_frame * | lintospeex_frameout (struct ast_trans_pvt *pvt) |
convert work buffer and produce output frame | |
static int | lintospeex_new (struct ast_trans_pvt *pvt) |
static int | load_module (void) |
static int | parse_config (int reload) |
static int | reload (void) |
static void | speextolin_destroy (struct ast_trans_pvt *arg) |
static int | speextolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f) |
convert and store into outbuf | |
static int | speextolin_new (struct ast_trans_pvt *pvt) |
static int | unload_module (void) |
Variables | |
static struct ast_module_info __MODULE_INFO_SECTION | __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Speex Coder/Decoder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } |
static int | abr = 0 |
static struct ast_module_info * | ast_module_info = &__mod_info |
static int | complexity = 2 |
static int | dtx = 0 |
static int | enhancement = 0 |
static struct ast_translator | lintospeex |
static int | pp_agc = 0 |
static float | pp_agc_level = 8000 |
static int | pp_denoise = 0 |
static int | pp_dereverb = 0 |
static float | pp_dereverb_decay = 0.4 |
static float | pp_dereverb_level = 0.3 |
static int | pp_vad = 0 |
static int | preproc = 0 |
static int | quality = 3 |
static struct ast_translator | speextolin |
static int | vad = 0 |
static int | vbr = 0 |
static float | vbr_quality = 4 |
Translate between signed linear and Speex (Open Codec)
Definition in file codec_speex.c.
#define BUFFER_SAMPLES 8000 |
Definition at line 80 of file codec_speex.c.
Referenced by speextolin_framein().
#define SPEEX_SAMPLES 160 |
Definition at line 81 of file codec_speex.c.
Referenced by speex_sample().
#define TYPE_HIGH 0x0 |
Definition at line 76 of file codec_speex.c.
#define TYPE_LOW 0x1 |
Definition at line 77 of file codec_speex.c.
#define TYPE_MASK 0x3 |
Definition at line 78 of file codec_speex.c.
#define TYPE_SILENCE 0x2 |
Definition at line 75 of file codec_speex.c.
static void __reg_module | ( | void | ) | [static] |
Definition at line 472 of file codec_speex.c.
static void __unreg_module | ( | void | ) | [static] |
Definition at line 472 of file codec_speex.c.
static void lintospeex_destroy | ( | struct ast_trans_pvt * | arg | ) | [static] |
Definition at line 301 of file codec_speex.c.
References speex_coder_pvt::bits, ast_trans_pvt::pvt, and speex_coder_pvt::speex.
{ struct speex_coder_pvt *pvt = arg->pvt; #ifdef _SPEEX_TYPES_H if (preproc) speex_preprocess_state_destroy(pvt->pp); #endif speex_encoder_destroy(pvt->speex); speex_bits_destroy(&pvt->bits); }
static int lintospeex_framein | ( | struct ast_trans_pvt * | pvt, |
struct ast_frame * | f | ||
) | [static] |
store input frame in work buffer
Definition at line 215 of file codec_speex.c.
References speex_coder_pvt::buf, ast_frame::data, ast_frame::datalen, ast_frame::ptr, ast_trans_pvt::pvt, ast_frame::samples, and ast_trans_pvt::samples.
{ struct speex_coder_pvt *tmp = pvt->pvt; /* XXX We should look at how old the rest of our stream is, and if it is too old, then we should overwrite it entirely, otherwise we can get artifacts of earlier talk that do not belong */ memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen); pvt->samples += f->samples; return 0; }
static struct ast_frame* lintospeex_frameout | ( | struct ast_trans_pvt * | pvt | ) | [static, read] |
convert work buffer and produce output frame
Definition at line 228 of file codec_speex.c.
References AST_FRAME_CNG, ast_trans_frameout(), speex_coder_pvt::bits, speex_coder_pvt::buf, ast_translator::buf_size, ast_trans_pvt::c, dtx, ast_trans_pvt::f, speex_coder_pvt::framesize, ast_frame::frametype, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, ast_frame::samples, ast_trans_pvt::samples, speex_coder_pvt::silent_state, speex_coder_pvt::speex, and ast_trans_pvt::t.
{ struct speex_coder_pvt *tmp = pvt->pvt; int is_speech=1; int datalen = 0; /* output bytes */ int samples = 0; /* output samples */ /* We can't work on anything less than a frame in size */ if (pvt->samples < tmp->framesize) return NULL; speex_bits_reset(&tmp->bits); while (pvt->samples >= tmp->framesize) { #ifdef _SPEEX_TYPES_H /* Preprocess audio */ if (preproc) is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL); /* Encode a frame of data */ if (is_speech) { /* If DTX enabled speex_encode returns 0 during silence */ is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx; } else { /* 5 zeros interpreted by Speex as silence (submode 0) */ speex_bits_pack(&tmp->bits, 0, 5); } #else { float fbuf[1024]; int x; /* Convert to floating point */ for (x = 0; x < tmp->framesize; x++) fbuf[x] = tmp->buf[samples + x]; /* Encode a frame of data */ is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx; } #endif samples += tmp->framesize; pvt->samples -= tmp->framesize; } /* Move the data at the end of the buffer to the front */ if (pvt->samples) memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); /* Use AST_FRAME_CNG to signify the start of any silence period */ if (is_speech) { tmp->silent_state = 0; } else { if (tmp->silent_state) { return NULL; } else { tmp->silent_state = 1; speex_bits_reset(&tmp->bits); memset(&pvt->f, 0, sizeof(pvt->f)); pvt->f.frametype = AST_FRAME_CNG; pvt->f.samples = samples; /* XXX what now ? format etc... */ } } /* Terminate bit stream */ speex_bits_pack(&tmp->bits, 15, 5); datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size); return ast_trans_frameout(pvt, datalen, samples); }
static int lintospeex_new | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 101 of file codec_speex.c.
References speex_coder_pvt::bits, speex_coder_pvt::framesize, ast_trans_pvt::pvt, speex_coder_pvt::silent_state, and speex_coder_pvt::speex.
{ struct speex_coder_pvt *tmp = pvt->pvt; if (!(tmp->speex = speex_encoder_init(&speex_nb_mode))) return -1; speex_bits_init(&tmp->bits); speex_bits_reset(&tmp->bits); speex_encoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize); speex_encoder_ctl(tmp->speex, SPEEX_SET_COMPLEXITY, &complexity); #ifdef _SPEEX_TYPES_H if (preproc) { tmp->pp = speex_preprocess_state_init(tmp->framesize, 8000); /* XXX what is this 8000 ? */ speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_VAD, &pp_vad); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC, &pp_agc); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &pp_agc_level); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DENOISE, &pp_denoise); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB, &pp_dereverb); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &pp_dereverb_decay); speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &pp_dereverb_level); } #endif if (!abr && !vbr) { speex_encoder_ctl(tmp->speex, SPEEX_SET_QUALITY, &quality); if (vad) speex_encoder_ctl(tmp->speex, SPEEX_SET_VAD, &vad); } if (vbr) { speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR, &vbr); speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR_QUALITY, &vbr_quality); } if (abr) speex_encoder_ctl(tmp->speex, SPEEX_SET_ABR, &abr); if (dtx) speex_encoder_ctl(tmp->speex, SPEEX_SET_DTX, &dtx); tmp->silent_state = 0; return 0; }
static int load_module | ( | void | ) | [static] |
Definition at line 452 of file codec_speex.c.
References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, ast_unregister_translator(), and parse_config().
{ int res; if (parse_config(0)) return AST_MODULE_LOAD_DECLINE; res=ast_register_translator(&speextolin); if (!res) res=ast_register_translator(&lintospeex); else ast_unregister_translator(&speextolin); if (res) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; }
static int parse_config | ( | int | reload | ) | [static] |
Definition at line 340 of file codec_speex.c.
References ast_config_destroy(), ast_config_load, ast_log(), ast_true(), ast_variable_browse(), ast_verb, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, ast_variable::name, ast_variable::next, ast_variable::value, and var.
Referenced by load_module(), and reload().
{ struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; struct ast_config *cfg = ast_config_load("codecs.conf", config_flags); struct ast_variable *var; int res; float res_f; if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) return 0; for (var = ast_variable_browse(cfg, "speex"); var; var = var->next) { if (!strcasecmp(var->name, "quality")) { res = abs(atoi(var->value)); if (res > -1 && res < 11) { ast_verb(3, "CODEC SPEEX: Setting Quality to %d\n",res); quality = res; } else ast_log(LOG_ERROR,"Error Quality must be 0-10\n"); } else if (!strcasecmp(var->name, "complexity")) { res = abs(atoi(var->value)); if (res > -1 && res < 11) { ast_verb(3, "CODEC SPEEX: Setting Complexity to %d\n",res); complexity = res; } else ast_log(LOG_ERROR,"Error! Complexity must be 0-10\n"); } else if (!strcasecmp(var->name, "vbr_quality")) { if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0 && res_f <= 10) { ast_verb(3, "CODEC SPEEX: Setting VBR Quality to %f\n",res_f); vbr_quality = res_f; } else ast_log(LOG_ERROR,"Error! VBR Quality must be 0-10\n"); } else if (!strcasecmp(var->name, "abr_quality")) { ast_log(LOG_ERROR,"Error! ABR Quality setting obsolete, set ABR to desired bitrate\n"); } else if (!strcasecmp(var->name, "enhancement")) { enhancement = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ? "on" : "off"); } else if (!strcasecmp(var->name, "vbr")) { vbr = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: VBR Mode. [%s]\n",vbr ? "on" : "off"); } else if (!strcasecmp(var->name, "abr")) { res = abs(atoi(var->value)); if (res >= 0) { if (res > 0) ast_verb(3, "CODEC SPEEX: Setting ABR target bitrate to %d\n",res); else ast_verb(3, "CODEC SPEEX: Disabling ABR\n"); abr = res; } else ast_log(LOG_ERROR,"Error! ABR target bitrate must be >= 0\n"); } else if (!strcasecmp(var->name, "vad")) { vad = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: VAD Mode. [%s]\n",vad ? "on" : "off"); } else if (!strcasecmp(var->name, "dtx")) { dtx = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: DTX Mode. [%s]\n",dtx ? "on" : "off"); } else if (!strcasecmp(var->name, "preprocess")) { preproc = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Preprocessing. [%s]\n",preproc ? "on" : "off"); } else if (!strcasecmp(var->name, "pp_vad")) { pp_vad = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Preprocessor VAD. [%s]\n",pp_vad ? "on" : "off"); } else if (!strcasecmp(var->name, "pp_agc")) { pp_agc = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ? "on" : "off"); } else if (!strcasecmp(var->name, "pp_agc_level")) { if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) { ast_verb(3, "CODEC SPEEX: Setting preprocessor AGC Level to %f\n",res_f); pp_agc_level = res_f; } else ast_log(LOG_ERROR,"Error! Preprocessor AGC Level must be >= 0\n"); } else if (!strcasecmp(var->name, "pp_denoise")) { pp_denoise = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Preprocessor Denoise. [%s]\n",pp_denoise ? "on" : "off"); } else if (!strcasecmp(var->name, "pp_dereverb")) { pp_dereverb = ast_true(var->value) ? 1 : 0; ast_verb(3, "CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ? "on" : "off"); } else if (!strcasecmp(var->name, "pp_dereverb_decay")) { if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) { ast_verb(3, "CODEC SPEEX: Setting preprocessor Dereverb Decay to %f\n",res_f); pp_dereverb_decay = res_f; } else ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Decay must be >= 0\n"); } else if (!strcasecmp(var->name, "pp_dereverb_level")) { if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) { ast_verb(3, "CODEC SPEEX: Setting preprocessor Dereverb Level to %f\n",res_f); pp_dereverb_level = res_f; } else ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Level must be >= 0\n"); } } ast_config_destroy(cfg); return 0; }
static int reload | ( | void | ) | [static] |
Definition at line 435 of file codec_speex.c.
References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and parse_config().
{ if (parse_config(1)) return AST_MODULE_LOAD_DECLINE; return AST_MODULE_LOAD_SUCCESS; }
static void speextolin_destroy | ( | struct ast_trans_pvt * | arg | ) | [static] |
Definition at line 293 of file codec_speex.c.
References speex_coder_pvt::bits, ast_trans_pvt::pvt, and speex_coder_pvt::speex.
{ struct speex_coder_pvt *pvt = arg->pvt; speex_decoder_destroy(pvt->speex); speex_bits_destroy(&pvt->bits); }
static int speextolin_framein | ( | struct ast_trans_pvt * | pvt, |
struct ast_frame * | f | ||
) | [static] |
convert and store into outbuf
Definition at line 158 of file codec_speex.c.
References ast_log(), speex_coder_pvt::bits, BUFFER_SAMPLES, ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, speex_coder_pvt::framesize, ast_trans_pvt::i16, LOG_WARNING, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, and speex_coder_pvt::speex.
{ struct speex_coder_pvt *tmp = pvt->pvt; /* Assuming there's space left, decode into the current buffer at the tail location. Read in as many frames as there are */ int x; int res; int16_t *dst = pvt->outbuf.i16; /* XXX fout is a temporary buffer, may have different types */ #ifdef _SPEEX_TYPES_H spx_int16_t fout[1024]; #else float fout[1024]; #endif if (f->datalen == 0) { /* Native PLC interpolation */ if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) { ast_log(LOG_WARNING, "Out of buffer space\n"); return -1; } #ifdef _SPEEX_TYPES_H speex_decode_int(tmp->speex, NULL, dst + pvt->samples); #else speex_decode(tmp->speex, NULL, fout); for (x=0;x<tmp->framesize;x++) { dst[pvt->samples + x] = (int16_t)fout[x]; } #endif pvt->samples += tmp->framesize; pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */ return 0; } /* Read in bits */ speex_bits_read_from(&tmp->bits, f->data.ptr, f->datalen); for (;;) { #ifdef _SPEEX_TYPES_H res = speex_decode_int(tmp->speex, &tmp->bits, fout); #else res = speex_decode(tmp->speex, &tmp->bits, fout); #endif if (res < 0) break; if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) { ast_log(LOG_WARNING, "Out of buffer space\n"); return -1; } for (x = 0 ; x < tmp->framesize; x++) dst[pvt->samples + x] = (int16_t)fout[x]; pvt->samples += tmp->framesize; pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */ } return 0; }
static int speextolin_new | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 142 of file codec_speex.c.
References speex_coder_pvt::bits, speex_coder_pvt::framesize, ast_trans_pvt::pvt, and speex_coder_pvt::speex.
{ struct speex_coder_pvt *tmp = pvt->pvt; if (!(tmp->speex = speex_decoder_init(&speex_nb_mode))) return -1; speex_bits_init(&tmp->bits); speex_decoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize); if (enhancement) speex_decoder_ctl(tmp->speex, SPEEX_SET_ENH, &enhancement); return 0; }
static int unload_module | ( | void | ) | [static] |
Definition at line 442 of file codec_speex.c.
References ast_unregister_translator().
{ int res; res = ast_unregister_translator(&lintospeex); res |= ast_unregister_translator(&speextolin); return res; }
struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Speex Coder/Decoder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static] |
Definition at line 472 of file codec_speex.c.
int abr = 0 [static] |
Definition at line 63 of file codec_speex.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 472 of file codec_speex.c.
int complexity = 2 [static] |
Definition at line 58 of file codec_speex.c.
int dtx = 0 [static] |
Definition at line 64 of file codec_speex.c.
Referenced by lintospeex_frameout().
int enhancement = 0 [static] |
Definition at line 59 of file codec_speex.c.
struct ast_translator lintospeex [static] |
Definition at line 326 of file codec_speex.c.
int pp_agc = 0 [static] |
Definition at line 68 of file codec_speex.c.
float pp_agc_level = 8000 [static] |
Definition at line 69 of file codec_speex.c.
int pp_denoise = 0 [static] |
Definition at line 70 of file codec_speex.c.
int pp_dereverb = 0 [static] |
Definition at line 71 of file codec_speex.c.
float pp_dereverb_decay = 0.4 [static] |
Definition at line 72 of file codec_speex.c.
float pp_dereverb_level = 0.3 [static] |
Definition at line 73 of file codec_speex.c.
int pp_vad = 0 [static] |
Definition at line 67 of file codec_speex.c.
int preproc = 0 [static] |
Definition at line 66 of file codec_speex.c.
int quality = 3 [static] |
Definition at line 57 of file codec_speex.c.
struct ast_translator speextolin [static] |
Definition at line 312 of file codec_speex.c.
int vad = 0 [static] |
Definition at line 60 of file codec_speex.c.
int vbr = 0 [static] |
Definition at line 61 of file codec_speex.c.
float vbr_quality = 4 [static] |
Definition at line 62 of file codec_speex.c.