| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-01-11 03:37:19 +00:00
										 |  |  |  * Copyright (C) 1999 - 2008, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Matthew Fredrickson <creslin@digium.com> | 
					
						
							| 
									
										
										
										
											2008-01-11 03:37:19 +00:00
										 |  |  |  * Russell Bryant <russell@digium.com> | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-12-21 00:11:00 +00:00
										 |  |  |  * Special thanks to Steve Underwood for the implementation | 
					
						
							|  |  |  |  * and for doing the 8khz<->g.722 direct translation code. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \file
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \brief codec_g722.c - translate between signed linear and ITU G.722-64kbps | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-01-11 03:37:19 +00:00
										 |  |  |  * \author Matthew Fredrickson <creslin@digium.com> | 
					
						
							|  |  |  |  * \author Russell Bryant <russell@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2007-12-27 16:11:41 +00:00
										 |  |  |  * \arg http://soft-switch.org/downloads/non-gpl-bits.tgz
 | 
					
						
							|  |  |  |  * \arg http://lists.digium.com/pipermail/asterisk-dev/2006-September/022866.html
 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  |  * \ingroup codecs | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/linkedlists.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/translate.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BUFFER_SAMPLES   8096	/* size for the translation buffers */
 | 
					
						
							|  |  |  | #define BUF_SHIFT	5
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "g722/g722.h"
 | 
					
						
							| 
									
										
										
										
											2008-10-17 21:35:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Sample frame data */ | 
					
						
							|  |  |  | #include "asterisk/slin.h"
 | 
					
						
							|  |  |  | #include "ex_g722.h"
 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct g722_encoder_pvt { | 
					
						
							|  |  |  | 	g722_encode_state_t g722; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct g722_decoder_pvt { | 
					
						
							|  |  |  | 	g722_decode_state_t g722; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief init a new instance of g722_encoder_pvt. */ | 
					
						
							|  |  |  | static int lintog722_new(struct ast_trans_pvt *pvt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct g722_encoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | static int lin16tog722_new(struct ast_trans_pvt *pvt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct g722_encoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g722_encode_init(&tmp->g722, 64000, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | /*! \brief init a new instance of g722_encoder_pvt. */ | 
					
						
							|  |  |  | static int g722tolin_new(struct ast_trans_pvt *pvt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct g722_decoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | static int g722tolin16_new(struct ast_trans_pvt *pvt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct g722_decoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g722_decode_init(&tmp->g722, 64000, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct g722_decoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 	int out_samples; | 
					
						
							| 
									
										
										
										
											2008-03-07 00:24:58 +00:00
										 |  |  | 	int in_samples; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* g722_decode expects the samples to be in the invalid samples / 2 format */ | 
					
						
							|  |  |  | 	in_samples = f->samples / 2; | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-26 17:06:17 +00:00
										 |  |  | 	out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)],  | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 		(uint8_t *) f->data.ptr, in_samples); | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pvt->samples += out_samples; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pvt->datalen += (out_samples * sizeof(int16_t)); | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct g722_encoder_pvt *tmp = pvt->pvt; | 
					
						
							|  |  |  | 	int outlen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-26 17:06:17 +00:00
										 |  |  | 	outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]),  | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 		(int16_t *) f->data.ptr, f->samples); | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-07 00:24:58 +00:00
										 |  |  | 	pvt->samples += outlen * 2; | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pvt->datalen += outlen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | static struct ast_translator g722tolin = { | 
					
						
							|  |  |  | 	.name = "g722tolin", | 
					
						
							|  |  |  | 	.srcfmt = AST_FORMAT_G722, | 
					
						
							|  |  |  | 	.dstfmt = AST_FORMAT_SLINEAR, | 
					
						
							|  |  |  | 	.newpvt = g722tolin_new,	/* same for both directions */ | 
					
						
							|  |  |  | 	.framein = g722tolin_framein, | 
					
						
							| 
									
										
										
										
											2008-10-17 21:35:23 +00:00
										 |  |  | 	.sample = g722_sample, | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 	.desc_size = sizeof(struct g722_decoder_pvt), | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 	.buffer_samples = BUFFER_SAMPLES / sizeof(int16_t), | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 	.buf_size = BUFFER_SAMPLES, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_translator lintog722 = { | 
					
						
							|  |  |  | 	.name = "lintog722", | 
					
						
							|  |  |  | 	.srcfmt = AST_FORMAT_SLINEAR, | 
					
						
							|  |  |  | 	.dstfmt = AST_FORMAT_G722, | 
					
						
							|  |  |  | 	.newpvt = lintog722_new,	/* same for both directions */ | 
					
						
							|  |  |  | 	.framein = lintog722_framein, | 
					
						
							| 
									
										
										
										
											2008-10-17 21:35:23 +00:00
										 |  |  | 	.sample = slin8_sample, | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 	.desc_size = sizeof(struct g722_encoder_pvt), | 
					
						
							| 
									
										
										
										
											2008-01-11 03:37:19 +00:00
										 |  |  | 	.buffer_samples = BUFFER_SAMPLES * 2, | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 	.buf_size = BUFFER_SAMPLES, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | static struct ast_translator g722tolin16 = { | 
					
						
							|  |  |  | 	.name = "g722tolin16", | 
					
						
							|  |  |  | 	.srcfmt = AST_FORMAT_G722, | 
					
						
							|  |  |  | 	.dstfmt = AST_FORMAT_SLINEAR16, | 
					
						
							|  |  |  | 	.newpvt = g722tolin16_new,	/* same for both directions */ | 
					
						
							| 
									
										
										
										
											2008-01-11 17:17:54 +00:00
										 |  |  | 	.framein = g722tolin_framein, | 
					
						
							| 
									
										
										
										
											2008-10-17 21:35:23 +00:00
										 |  |  | 	.sample = g722_sample, | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	.desc_size = sizeof(struct g722_decoder_pvt), | 
					
						
							| 
									
										
										
										
											2008-01-10 23:16:09 +00:00
										 |  |  | 	.buffer_samples = BUFFER_SAMPLES / sizeof(int16_t), | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	.buf_size = BUFFER_SAMPLES, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_translator lin16tog722 = { | 
					
						
							|  |  |  | 	.name = "lin16tog722", | 
					
						
							|  |  |  | 	.srcfmt = AST_FORMAT_SLINEAR16, | 
					
						
							|  |  |  | 	.dstfmt = AST_FORMAT_G722, | 
					
						
							|  |  |  | 	.newpvt = lin16tog722_new,	/* same for both directions */ | 
					
						
							| 
									
										
										
										
											2008-01-11 17:17:54 +00:00
										 |  |  | 	.framein = lintog722_framein, | 
					
						
							| 
									
										
										
										
											2008-10-17 21:35:23 +00:00
										 |  |  | 	.sample = slin16_sample, | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	.desc_size = sizeof(struct g722_encoder_pvt), | 
					
						
							| 
									
										
										
										
											2008-01-11 03:37:19 +00:00
										 |  |  | 	.buffer_samples = BUFFER_SAMPLES * 2, | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	.buf_size = BUFFER_SAMPLES, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | static int reload(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res |= ast_unregister_translator(&g722tolin); | 
					
						
							|  |  |  | 	res |= ast_unregister_translator(&lintog722); | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	res |= ast_unregister_translator(&g722tolin16); | 
					
						
							|  |  |  | 	res |= ast_unregister_translator(&lin16tog722); | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res |= ast_register_translator(&g722tolin); | 
					
						
							|  |  |  | 	res |= ast_register_translator(&lintog722); | 
					
						
							| 
									
										
										
										
											2007-11-06 22:51:48 +00:00
										 |  |  | 	res |= ast_register_translator(&g722tolin16); | 
					
						
							|  |  |  | 	res |= ast_register_translator(&lin16tog722); | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	if (res) { | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 		unload_module(); | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	}	 | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2006-12-21 00:08:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder", | 
					
						
							|  |  |  | 		.load = load_module, | 
					
						
							|  |  |  | 		.unload = unload_module, | 
					
						
							|  |  |  | 		.reload = reload, | 
					
						
							|  |  |  | 	       ); |