Remove unnecessary code relating to PLC.
[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 "asterisk/translate.h"
35 #include "asterisk/config.h"
36 #include "asterisk/module.h"
37 #include "asterisk/utils.h"
38
39 #include "lpc10/lpc10.h"
40
41 /* Sample frame data */
42 #include "asterisk/slin.h"
43 #include "ex_lpc10.h"
44
45 /* We use a very strange format here...  I have no idea why...  The frames are 180
46    samples long, which isn't even an even number of milliseconds...  Not only that
47    but we hvae to waste two bits of each frame to keep them ending on a byte boundary
48    because the frames are 54 bits long */
49
50 #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
51
52 #define BUFFER_SAMPLES  8000
53
54 struct lpc10_coder_pvt {
55         union {
56                 struct lpc10_encoder_state *enc;
57                 struct lpc10_decoder_state *dec;
58         } lpc10;
59         /* Enough to store a full second */
60         short buf[BUFFER_SAMPLES];
61         int longer;
62 };
63
64 static int lpc10_enc_new(struct ast_trans_pvt *pvt)
65 {
66         struct lpc10_coder_pvt *tmp = pvt->pvt;
67
68         return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
69 }
70
71 static int lpc10_dec_new(struct ast_trans_pvt *pvt)
72 {
73         struct lpc10_coder_pvt *tmp = pvt->pvt;
74
75         return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
76 }
77
78 static void extract_bits(INT32 *bits, unsigned char *c)
79 {
80         int x;
81         for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
82                 if (*c & (0x80 >> (x & 7)))
83                         bits[x] = 1;
84                 else
85                         bits[x] = 0;
86                 if ((x & 7) == 7)
87                         c++;
88         }
89 }
90
91 /* XXX note lpc10_encode() produces one bit per word in bits[] */
92 static void build_bits(unsigned char *c, INT32 *bits)
93 {
94         unsigned char mask=0x80;
95         int x;
96         *c = 0;
97         for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
98                 if (bits[x])
99                         *c |= mask;
100                 mask = mask >> 1;
101                 if ((x % 8)==7) {
102                         c++;
103                         *c = 0;
104                         mask = 0x80;
105                 }
106         }
107 }
108
109 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
110 {
111         struct lpc10_coder_pvt *tmp = pvt->pvt;
112         int16_t *dst = pvt->outbuf.i16;
113         int len = 0;
114
115         while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
116                 int x;
117                 float tmpbuf[LPC10_SAMPLES_PER_FRAME];
118                 INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
119                 if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
120                         ast_log(LOG_WARNING, "Out of buffer space\n");
121                         return -1;
122                 }
123                 extract_bits(bits, f->data.ptr + len);
124                 if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
125                         ast_log(LOG_WARNING, "Invalid lpc10 data\n");
126                         return -1;
127                 }
128                 for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
129                         /* Convert to a short between -1.0 and 1.0 */
130                         dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
131                 }
132
133                 pvt->samples += LPC10_SAMPLES_PER_FRAME;
134                 pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
135                 len += LPC10_BYTES_IN_COMPRESSED_FRAME;
136         }
137         if (len != f->datalen) 
138                 printf("Decoded %d, expected %d\n", len, f->datalen);
139         return 0;
140 }
141
142 static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
143 {
144         struct lpc10_coder_pvt *tmp = pvt->pvt;
145
146         /* Just add the frames to our stream */
147         if (pvt->samples + f->samples > BUFFER_SAMPLES) {
148                 ast_log(LOG_WARNING, "Out of buffer space\n");
149                 return -1;
150         }
151         memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
152         pvt->samples += f->samples;
153         return 0;
154 }
155
156 static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
157 {
158         struct lpc10_coder_pvt *tmp = pvt->pvt;
159         int x;
160         int datalen = 0;        /* output frame */
161         int samples = 0;        /* output samples */
162         float tmpbuf[LPC10_SAMPLES_PER_FRAME];
163         INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME];     /* XXX what ??? */
164         /* We can't work on anything less than a frame in size */
165         if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
166                 return NULL;
167         while (pvt->samples >=  LPC10_SAMPLES_PER_FRAME) {
168                 /* Encode a frame of data */
169                 for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
170                         tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
171                 lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
172                 build_bits(pvt->outbuf.uc + datalen, bits);
173                 datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
174                 samples += LPC10_SAMPLES_PER_FRAME;
175                 pvt->samples -= LPC10_SAMPLES_PER_FRAME;
176                 /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
177                    important for IAX use */
178                 tmp->longer = 1 - tmp->longer;
179         }
180         /* Move the data at the end of the buffer to the front */
181         if (pvt->samples)
182                 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
183         return ast_trans_frameout(pvt, datalen, samples);
184 }
185
186
187 static void lpc10_destroy(struct ast_trans_pvt *arg)
188 {
189         struct lpc10_coder_pvt *pvt = arg->pvt;
190         /* Enc and DEC are both just allocated, so they can be freed */
191         ast_free(pvt->lpc10.enc);
192 }
193
194 static struct ast_translator lpc10tolin = {
195         .name = "lpc10tolin", 
196         .srcfmt = AST_FORMAT_LPC10,
197         .dstfmt = AST_FORMAT_SLINEAR,
198         .newpvt = lpc10_dec_new,
199         .framein = lpc10tolin_framein,
200         .destroy = lpc10_destroy,
201         .sample = lpc10_sample,
202         .desc_size = sizeof(struct lpc10_coder_pvt),
203         .buffer_samples = BUFFER_SAMPLES,
204         .buf_size = BUFFER_SAMPLES * 2,
205 };
206
207 static struct ast_translator lintolpc10 = {
208         .name = "lintolpc10", 
209         .srcfmt = AST_FORMAT_SLINEAR,
210         .dstfmt = AST_FORMAT_LPC10,
211         .newpvt = lpc10_enc_new,
212         .framein = lintolpc10_framein,
213         .frameout = lintolpc10_frameout,
214         .destroy = lpc10_destroy,
215         .sample = slin8_sample,
216         .desc_size = sizeof(struct lpc10_coder_pvt),
217         .buffer_samples = BUFFER_SAMPLES,
218         .buf_size = LPC10_BYTES_IN_COMPRESSED_FRAME * (1 + BUFFER_SAMPLES / LPC10_SAMPLES_PER_FRAME),
219 };
220
221 static int reload(void)
222 {
223         return AST_MODULE_LOAD_SUCCESS;
224 }
225
226
227 static int unload_module(void)
228 {
229         int res;
230
231         res = ast_unregister_translator(&lintolpc10);
232         res |= ast_unregister_translator(&lpc10tolin);
233
234         return res;
235 }
236
237 static int load_module(void)
238 {
239         int res;
240
241         res = ast_register_translator(&lpc10tolin);
242         if (!res) 
243                 res = ast_register_translator(&lintolpc10);
244         else
245                 ast_unregister_translator(&lpc10tolin);
246         if (res)
247                 return AST_MODULE_LOAD_FAILURE;
248         return AST_MODULE_LOAD_SUCCESS;
249 }
250
251 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "LPC10 2.4kbps Coder/Decoder",
252                 .load = load_module,
253                 .unload = unload_module,
254                 .reload = reload,
255                );