2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Matthew Fredrickson <creslin@digium.com>
7 * Russell Bryant <russell@digium.com>
9 * Special thanks to Steve Underwood for the implementation
10 * and for doing the 8khz<->g.722 direct translation code.
12 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
18 * This program is free software, distributed under the terms of
19 * the GNU General Public License Version 2. See the LICENSE file
20 * at the top of the source tree.
25 * \brief codec_g722.c - translate between signed linear and ITU G.722-64kbps
27 * \author Matthew Fredrickson <creslin@digium.com>
28 * \author Russell Bryant <russell@digium.com>
30 * \arg http://soft-switch.org/downloads/non-gpl-bits.tgz
31 * \arg http://lists.digium.com/pipermail/asterisk-dev/2006-September/022866.html
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/module.h"
42 #include "asterisk/config.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/utils.h"
46 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
49 #include "g722/g722.h"
51 /* Sample frame data */
52 #include "asterisk/slin.h"
55 struct g722_encoder_pvt {
56 g722_encode_state_t g722;
59 struct g722_decoder_pvt {
60 g722_decode_state_t g722;
63 /*! \brief init a new instance of g722_encoder_pvt. */
64 static int lintog722_new(struct ast_trans_pvt *pvt)
66 struct g722_encoder_pvt *tmp = pvt->pvt;
68 g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
73 static int lin16tog722_new(struct ast_trans_pvt *pvt)
75 struct g722_encoder_pvt *tmp = pvt->pvt;
77 g722_encode_init(&tmp->g722, 64000, 0);
82 /*! \brief init a new instance of g722_encoder_pvt. */
83 static int g722tolin_new(struct ast_trans_pvt *pvt)
85 struct g722_decoder_pvt *tmp = pvt->pvt;
87 g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
92 static int g722tolin16_new(struct ast_trans_pvt *pvt)
94 struct g722_decoder_pvt *tmp = pvt->pvt;
96 g722_decode_init(&tmp->g722, 64000, 0);
101 static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
103 struct g722_decoder_pvt *tmp = pvt->pvt;
107 /* g722_decode expects the samples to be in the invalid samples / 2 format */
108 in_samples = f->samples / 2;
110 out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)],
111 (uint8_t *) f->data.ptr, in_samples);
113 pvt->samples += out_samples;
115 pvt->datalen += (out_samples * sizeof(int16_t));
120 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
122 struct g722_encoder_pvt *tmp = pvt->pvt;
125 outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]),
126 (int16_t *) f->data.ptr, f->samples);
128 pvt->samples += outlen * 2;
130 pvt->datalen += outlen;
135 static struct ast_translator g722tolin = {
137 .srcfmt = AST_FORMAT_G722,
138 .dstfmt = AST_FORMAT_SLINEAR,
139 .newpvt = g722tolin_new, /* same for both directions */
140 .framein = g722tolin_framein,
141 .sample = g722_sample,
142 .desc_size = sizeof(struct g722_decoder_pvt),
143 .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
144 .buf_size = BUFFER_SAMPLES,
147 static struct ast_translator lintog722 = {
149 .srcfmt = AST_FORMAT_SLINEAR,
150 .dstfmt = AST_FORMAT_G722,
151 .newpvt = lintog722_new, /* same for both directions */
152 .framein = lintog722_framein,
153 .sample = slin8_sample,
154 .desc_size = sizeof(struct g722_encoder_pvt),
155 .buffer_samples = BUFFER_SAMPLES * 2,
156 .buf_size = BUFFER_SAMPLES,
159 static struct ast_translator g722tolin16 = {
160 .name = "g722tolin16",
161 .srcfmt = AST_FORMAT_G722,
162 .dstfmt = AST_FORMAT_SLINEAR16,
163 .newpvt = g722tolin16_new, /* same for both directions */
164 .framein = g722tolin_framein,
165 .sample = g722_sample,
166 .desc_size = sizeof(struct g722_decoder_pvt),
167 .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
168 .buf_size = BUFFER_SAMPLES,
171 static struct ast_translator lin16tog722 = {
172 .name = "lin16tog722",
173 .srcfmt = AST_FORMAT_SLINEAR16,
174 .dstfmt = AST_FORMAT_G722,
175 .newpvt = lin16tog722_new, /* same for both directions */
176 .framein = lintog722_framein,
177 .sample = slin16_sample,
178 .desc_size = sizeof(struct g722_encoder_pvt),
179 .buffer_samples = BUFFER_SAMPLES * 2,
180 .buf_size = BUFFER_SAMPLES,
183 static int reload(void)
185 return AST_MODULE_LOAD_SUCCESS;
188 static int unload_module(void)
192 res |= ast_unregister_translator(&g722tolin);
193 res |= ast_unregister_translator(&lintog722);
194 res |= ast_unregister_translator(&g722tolin16);
195 res |= ast_unregister_translator(&lin16tog722);
200 static int load_module(void)
204 res |= ast_register_translator(&g722tolin);
205 res |= ast_register_translator(&lintog722);
206 res |= ast_register_translator(&g722tolin16);
207 res |= ast_register_translator(&lin16tog722);
211 return AST_MODULE_LOAD_FAILURE;
214 return AST_MODULE_LOAD_SUCCESS;
217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
219 .unload = unload_module,