e41137de1b91c5afaa2893acedcc5b21bbe867d6
[asterisk/asterisk.git] / codecs / codec_lpc10.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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/ 
11  *
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.
17  *
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.
21  */
22
23 /*! \file
24  *
25  * \brief Translate between signed linear and LPC10 (Linear Predictor Code)
26  *
27  * \ingroup codecs
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
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>
40
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"
49
50 #include "lpc10/lpc10.h"
51
52 /* Sample frame data */
53 #include "slin_lpc10_ex.h"
54 #include "lpc10_slin_ex.h"
55
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 */
60
61 #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
62
63 #define BUFFER_SAMPLES  8000
64
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;
73 };
74
75 static int lpc10_enc_new(struct ast_trans_pvt *pvt)
76 {
77         struct lpc10_coder_pvt *tmp = pvt->pvt;
78
79         return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
80 }
81
82 static int lpc10_dec_new(struct ast_trans_pvt *pvt)
83 {
84         struct lpc10_coder_pvt *tmp = pvt->pvt;
85
86         return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
87 }
88
89 static struct ast_frame *lintolpc10_sample(void)
90 {
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;
102 }
103
104 static struct ast_frame *lpc10tolin_sample(void)
105 {
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;
118 }
119
120 static void extract_bits(INT32 *bits, unsigned char *c)
121 {
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++;
130         }
131 }
132
133 /* XXX note lpc10_encode() produces one bit per word in bits[] */
134 static void build_bits(unsigned char *c, INT32 *bits)
135 {
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;
147                 }
148         }
149 }
150
151 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
152 {
153         struct lpc10_coder_pvt *tmp = pvt->pvt;
154         int16_t *dst = (int16_t *)pvt->outbuf;
155         int len = 0;
156
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;
164                 }
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;
169                 }
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]);
173                 }
174
175                 pvt->samples += LPC10_SAMPLES_PER_FRAME;
176                 pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
177                 len += LPC10_BYTES_IN_COMPRESSED_FRAME;
178         }
179         if (len != f->datalen) 
180                 printf("Decoded %d, expected %d\n", len, f->datalen);
181         return 0;
182 }
183
184 static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
185 {
186         struct lpc10_coder_pvt *tmp = pvt->pvt;
187
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;
192         }
193         memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
194         pvt->samples += f->samples;
195         return 0;
196 }
197
198 static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
199 {
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(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;
221 #if 0   /* what the heck was this for? */
222                 ((char *)(tmp->f.data))[consumed - 1] |= tmp->longer;
223 #endif          
224         }
225         /* Move the data at the end of the buffer to the front */
226         if (pvt->samples)
227                 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
228         return ast_trans_frameout(pvt, datalen, samples);
229 }
230
231
232 static void lpc10_destroy(struct ast_trans_pvt *arg)
233 {
234         struct lpc10_coder_pvt *pvt = arg->pvt;
235         /* Enc and DEC are both just allocated, so they can be freed */
236         free(pvt->lpc10.enc);
237 }
238
239 static struct ast_translator lpc10tolin = {
240         .name = "lpc10tolin", 
241         .srcfmt = AST_FORMAT_LPC10,
242         .dstfmt = AST_FORMAT_SLINEAR,
243         .newpvt = lpc10_dec_new,
244         .framein = lpc10tolin_framein,
245         .destroy = lpc10_destroy,
246         .sample = lpc10tolin_sample,
247         .desc_size = sizeof(struct lpc10_coder_pvt),
248         .buffer_samples = BUFFER_SAMPLES,
249         .plc_samples = LPC10_SAMPLES_PER_FRAME,
250         .buf_size = BUFFER_SAMPLES * 2,
251 };
252
253 static struct ast_translator lintolpc10 = {
254         .name = "lintolpc10", 
255         .srcfmt = AST_FORMAT_SLINEAR,
256         .dstfmt = AST_FORMAT_LPC10,
257         .newpvt = lpc10_enc_new,
258         .framein = lintolpc10_framein,
259         .frameout = lintolpc10_frameout,
260         .destroy = lpc10_destroy,
261         .sample = lintolpc10_sample,
262         .desc_size = sizeof(struct lpc10_coder_pvt),
263         .buffer_samples = BUFFER_SAMPLES,
264         .buf_size = LPC10_BYTES_IN_COMPRESSED_FRAME * (1 + BUFFER_SAMPLES / LPC10_SAMPLES_PER_FRAME),
265 };
266
267 static void parse_config(void)
268 {
269         struct ast_variable *var;
270         struct ast_config *cfg = ast_config_load("codecs.conf");
271         if (!cfg)
272                 return;
273         for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
274                if (!strcasecmp(var->name, "genericplc")) {
275                         lpc10tolin.useplc = ast_true(var->value) ? 1 : 0;
276                         if (option_verbose > 2)
277                                ast_verbose(VERBOSE_PREFIX_3 "codec_lpc10: %susing generic PLC\n",
278                                         lpc10tolin.useplc ? "" : "not ");
279                 }
280         }
281         ast_config_destroy(cfg);
282 }
283
284 static int reload(void *mod)
285 {
286         parse_config();
287         return 0;
288 }
289
290
291 static int unload_module(void *mod)
292 {
293         int res;
294         res = ast_unregister_translator(&lintolpc10);
295         res |= ast_unregister_translator(&lpc10tolin);
296         return res;
297 }
298
299 static int load_module(void *mod)
300 {
301         int res;
302         parse_config();
303         res=ast_register_translator(&lpc10tolin, mod);
304         if (!res) 
305                 res=ast_register_translator(&lintolpc10, mod);
306         else
307                 ast_unregister_translator(&lpc10tolin);
308         return res;
309 }
310
311 static const char *description(void)
312 {
313         return "LPC10 2.4kbps (signed linear) Voice Coder";
314 }
315
316 static const char *key(void)
317 {
318         return ASTERISK_GPL_KEY;
319 }
320
321 STD_MOD(MOD_1, reload, NULL, NULL);