ensure that DAHDI_INCLUDE and ZAPTEL_INCLUDE are added in all the places needed
[asterisk-bristuff.git] / codecs / codec_lpc10.c
blob3f46e0af8c42daffdb8927f253d1651212724d5c
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * The lpc10 code is from a library used by nautilus, modified to be a bit
9 * nicer to the compiler.
10 * See http://www.arl.wustl.edu/~jaf/
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.
23 /*! \file
25 * \brief Translate between signed linear and LPC10 (Linear Predictor Code)
27 * \ingroup codecs
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <netinet/in.h>
38 #include <string.h>
39 #include <stdio.h>
41 #include "asterisk/lock.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/config.h"
44 #include "asterisk/options.h"
45 #include "asterisk/module.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/utils.h"
50 #include "lpc10/lpc10.h"
52 /* Sample frame data */
53 #include "slin_lpc10_ex.h"
54 #include "lpc10_slin_ex.h"
56 /* We use a very strange format here... I have no idea why... The frames are 180
57 samples long, which isn't even an even number of milliseconds... Not only that
58 but we hvae to waste two bits of each frame to keep them ending on a byte boundary
59 because the frames are 54 bits long */
61 #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
63 #define BUFFER_SAMPLES 8000
65 struct lpc10_coder_pvt {
66 union {
67 struct lpc10_encoder_state *enc;
68 struct lpc10_decoder_state *dec;
69 } lpc10;
70 /* Enough to store a full second */
71 short buf[BUFFER_SAMPLES];
72 int longer;
75 static int lpc10_enc_new(struct ast_trans_pvt *pvt)
77 struct lpc10_coder_pvt *tmp = pvt->pvt;
79 return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
82 static int lpc10_dec_new(struct ast_trans_pvt *pvt)
84 struct lpc10_coder_pvt *tmp = pvt->pvt;
86 return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
89 static struct ast_frame *lintolpc10_sample(void)
91 static struct ast_frame f;
92 f.frametype = AST_FRAME_VOICE;
93 f.subclass = AST_FORMAT_SLINEAR;
94 f.datalen = sizeof(slin_lpc10_ex);
95 /* Assume 8000 Hz */
96 f.samples = LPC10_SAMPLES_PER_FRAME;
97 f.mallocd = 0;
98 f.offset = 0;
99 f.src = __PRETTY_FUNCTION__;
100 f.data = slin_lpc10_ex;
101 return &f;
104 static struct ast_frame *lpc10tolin_sample(void)
106 static struct ast_frame f;
107 f.frametype = AST_FRAME_VOICE;
108 f.subclass = AST_FORMAT_LPC10;
109 f.datalen = sizeof(lpc10_slin_ex);
110 /* All frames are 22 ms long (maybe a little more -- why did he choose
111 LPC10_SAMPLES_PER_FRAME sample frames anyway?? */
112 f.samples = LPC10_SAMPLES_PER_FRAME;
113 f.mallocd = 0;
114 f.offset = 0;
115 f.src = __PRETTY_FUNCTION__;
116 f.data = lpc10_slin_ex;
117 return &f;
120 static void extract_bits(INT32 *bits, unsigned char *c)
122 int x;
123 for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
124 if (*c & (0x80 >> (x & 7)))
125 bits[x] = 1;
126 else
127 bits[x] = 0;
128 if ((x & 7) == 7)
129 c++;
133 /* XXX note lpc10_encode() produces one bit per word in bits[] */
134 static void build_bits(unsigned char *c, INT32 *bits)
136 unsigned char mask=0x80;
137 int x;
138 *c = 0;
139 for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
140 if (bits[x])
141 *c |= mask;
142 mask = mask >> 1;
143 if ((x % 8)==7) {
144 c++;
145 *c = 0;
146 mask = 0x80;
151 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
153 struct lpc10_coder_pvt *tmp = pvt->pvt;
154 int16_t *dst = (int16_t *)pvt->outbuf;
155 int len = 0;
157 while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
158 int x;
159 float tmpbuf[LPC10_SAMPLES_PER_FRAME];
160 INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
161 if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
162 ast_log(LOG_WARNING, "Out of buffer space\n");
163 return -1;
165 extract_bits(bits, f->data + len);
166 if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
167 ast_log(LOG_WARNING, "Invalid lpc10 data\n");
168 return -1;
170 for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
171 /* Convert to a short between -1.0 and 1.0 */
172 dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
175 pvt->samples += LPC10_SAMPLES_PER_FRAME;
176 pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
177 len += LPC10_BYTES_IN_COMPRESSED_FRAME;
179 if (len != f->datalen)
180 printf("Decoded %d, expected %d\n", len, f->datalen);
181 return 0;
184 static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
186 struct lpc10_coder_pvt *tmp = pvt->pvt;
188 /* Just add the frames to our stream */
189 if (pvt->samples + f->samples > BUFFER_SAMPLES) {
190 ast_log(LOG_WARNING, "Out of buffer space\n");
191 return -1;
193 memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
194 pvt->samples += f->samples;
195 return 0;
198 static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
200 struct lpc10_coder_pvt *tmp = pvt->pvt;
201 int x;
202 int datalen = 0; /* output frame */
203 int samples = 0; /* output samples */
204 float tmpbuf[LPC10_SAMPLES_PER_FRAME];
205 INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX what ??? */
206 /* We can't work on anything less than a frame in size */
207 if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
208 return NULL;
209 while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) {
210 /* Encode a frame of data */
211 for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
212 tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
213 lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
214 build_bits((unsigned char *) pvt->outbuf + datalen, bits);
215 datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
216 samples += LPC10_SAMPLES_PER_FRAME;
217 pvt->samples -= LPC10_SAMPLES_PER_FRAME;
218 /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
219 important for IAX use */
220 tmp->longer = 1 - tmp->longer;
222 /* Move the data at the end of the buffer to the front */
223 if (pvt->samples)
224 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
225 return ast_trans_frameout(pvt, datalen, samples);
229 static void lpc10_destroy(struct ast_trans_pvt *arg)
231 struct lpc10_coder_pvt *pvt = arg->pvt;
232 /* Enc and DEC are both just allocated, so they can be freed */
233 free(pvt->lpc10.enc);
236 static struct ast_translator lpc10tolin = {
237 .name = "lpc10tolin",
238 .srcfmt = AST_FORMAT_LPC10,
239 .dstfmt = AST_FORMAT_SLINEAR,
240 .newpvt = lpc10_dec_new,
241 .framein = lpc10tolin_framein,
242 .destroy = lpc10_destroy,
243 .sample = lpc10tolin_sample,
244 .desc_size = sizeof(struct lpc10_coder_pvt),
245 .buffer_samples = BUFFER_SAMPLES,
246 .plc_samples = LPC10_SAMPLES_PER_FRAME,
247 .buf_size = BUFFER_SAMPLES * 2,
250 static struct ast_translator lintolpc10 = {
251 .name = "lintolpc10",
252 .srcfmt = AST_FORMAT_SLINEAR,
253 .dstfmt = AST_FORMAT_LPC10,
254 .newpvt = lpc10_enc_new,
255 .framein = lintolpc10_framein,
256 .frameout = lintolpc10_frameout,
257 .destroy = lpc10_destroy,
258 .sample = lintolpc10_sample,
259 .desc_size = sizeof(struct lpc10_coder_pvt),
260 .buffer_samples = BUFFER_SAMPLES,
261 .buf_size = LPC10_BYTES_IN_COMPRESSED_FRAME * (1 + BUFFER_SAMPLES / LPC10_SAMPLES_PER_FRAME),
264 static void parse_config(void)
266 struct ast_variable *var;
267 struct ast_config *cfg = ast_config_load("codecs.conf");
268 if (!cfg)
269 return;
270 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
271 if (!strcasecmp(var->name, "genericplc")) {
272 lpc10tolin.useplc = ast_true(var->value) ? 1 : 0;
273 if (option_verbose > 2)
274 ast_verbose(VERBOSE_PREFIX_3 "codec_lpc10: %susing generic PLC\n",
275 lpc10tolin.useplc ? "" : "not ");
278 ast_config_destroy(cfg);
281 static int reload(void)
283 parse_config();
285 return 0;
289 static int unload_module(void)
291 int res;
293 res = ast_unregister_translator(&lintolpc10);
294 res |= ast_unregister_translator(&lpc10tolin);
296 return res;
299 static int load_module(void)
301 int res;
303 parse_config();
304 res=ast_register_translator(&lpc10tolin);
305 if (!res)
306 res=ast_register_translator(&lintolpc10);
307 else
308 ast_unregister_translator(&lpc10tolin);
310 return res;
313 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "LPC10 2.4kbps Coder/Decoder",
314 .load = load_module,
315 .unload = unload_module,
316 .reload = reload,