⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codec_speex.c

📁 IP04是一个使用Blackfin开源硬件结合Asterisk开源软件建立的IPPBX系统.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*! \brief store input frame in work buffer */static int lintospeex_framein(struct ast_trans_pvt *pvt, struct ast_frame *f){	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, f->datalen);	pvt->samples += f->samples;	return 0;}/*! \brief convert work buffer and produce output frame */static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt){	struct speex_coder_pvt *tmp = pvt->pvt;	int is_speech=1;	int datalen = 0;	/* output bytes */	int samples = 0;	/* output samples */	unsigned int t;	/* 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 */			t = cycles();			is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx;			total_enc_cycles += cycles() - t;			if (enc_calls++ >= 200) {				unsigned int total_cycles = cycles() - start_enc_cycles;				start_enc_cycles = cycles();				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_3 "speex enc_calls  %d  total_cycles: %d  "						    "total_enc_cycles: %d enc CPU load: %6.3f%%\n", 						    enc_calls, total_cycles, total_enc_cycles, 						    100.0*(float)total_enc_cycles/total_cycles);				total_enc_cycles = 0;				enc_calls = 0;			}		} 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... */		}	}	datalen = speex_bits_write(&tmp->bits, pvt->outbuf, pvt->t->buf_size);	return ast_trans_frameout(pvt, datalen, samples);}static void speextolin_destroy(struct ast_trans_pvt *arg){	struct speex_coder_pvt *pvt = arg->pvt;	speex_decoder_destroy(pvt->speex);	speex_bits_destroy(&pvt->bits);}static void lintospeex_destroy(struct ast_trans_pvt *arg){	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 struct ast_translator speextolin = {	.name = "speextolin", 	.srcfmt = AST_FORMAT_SPEEX,	.dstfmt =  AST_FORMAT_SLINEAR,	.newpvt = speextolin_new,	.framein = speextolin_framein,	.destroy = speextolin_destroy,	.sample = speextolin_sample,	.desc_size = sizeof(struct speex_coder_pvt),	.buffer_samples = BUFFER_SAMPLES,	.buf_size = BUFFER_SAMPLES * 2,};static struct ast_translator lintospeex = {	.name = "lintospeex", 	.srcfmt = AST_FORMAT_SLINEAR,	.dstfmt = AST_FORMAT_SPEEX,	.newpvt = lintospeex_new,	.framein = lintospeex_framein,	.frameout = lintospeex_frameout,	.destroy = lintospeex_destroy,	.sample = lintospeex_sample,	.desc_size = sizeof(struct speex_coder_pvt),	.buffer_samples = BUFFER_SAMPLES,	.buf_size = BUFFER_SAMPLES * 2, /* XXX maybe a lot less ? */};static void parse_config(void) {	struct ast_config *cfg = ast_config_load("codecs.conf");	struct ast_variable *var;	int res;	float res_f;	if (cfg == NULL)		return;	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) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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, "%f", &res_f) == 1 && res_f >= 0 && res_f <= 10) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ? "on" : "off");		} else if (!strcasecmp(var->name, "vbr")) {			vbr = ast_true(var->value) ? 1 : 0;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_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 (option_verbose > 2) {					if (res > 0)						ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting ABR target bitrate to %d\n",res);					else						ast_verbose(VERBOSE_PREFIX_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;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: VAD Mode. [%s]\n",vad ? "on" : "off");		} else if (!strcasecmp(var->name, "dtx")) {			dtx = ast_true(var->value) ? 1 : 0;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: DTX Mode. [%s]\n",dtx ? "on" : "off");		} else if (!strcasecmp(var->name, "preprocess")) {			preproc = ast_true(var->value) ? 1 : 0;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessing. [%s]\n",preproc ? "on" : "off");		} else if (!strcasecmp(var->name, "pp_vad")) {			pp_vad = ast_true(var->value) ? 1 : 0;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_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;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ? "on" : "off");		} else if (!strcasecmp(var->name, "pp_agc_level")) {			if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_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;			if (option_verbose > 2)				ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ? "on" : "off");		} else if (!strcasecmp(var->name, "pp_dereverb_decay")) {			if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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, "%f", &res_f) == 1 && res_f >= 0) {				if (option_verbose > 2)					ast_verbose(VERBOSE_PREFIX_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);}static int reload(void) {	parse_config();	return 0;}static int unload_module(void){	int res;	res = ast_unregister_translator(&lintospeex);	res |= ast_unregister_translator(&speextolin);	return res;}static int load_module(void){	int res;	parse_config();	res=ast_register_translator(&speextolin);	if (!res) 		res=ast_register_translator(&lintospeex);	else		ast_unregister_translator(&speextolin);	return res;}AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Speex Coder/Decoder",		.load = load_module,		.unload = unload_module,		.reload = reload,	       );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -