pointer signedness warnings
[asterisk/asterisk.git] / codecs / codec_ilbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
5  *
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Translate between signed linear and Internet Low Bitrate Codec
24  * 
25  * \ingroup codecs
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <netinet/in.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include "asterisk/lock.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/module.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/utils.h"
45
46 #include "ilbc/iLBC_encode.h"
47 #include "ilbc/iLBC_decode.h"
48
49 /* Sample frame data */
50 #include "slin_ilbc_ex.h"
51 #include "ilbc_slin_ex.h"
52
53 #define USE_ILBC_ENHANCER       0
54 #define ILBC_MS                         30
55 /* #define ILBC_MS                      20 */
56
57 #define ILBC_FRAME_LEN  50      /* apparently... */
58 #define ILBC_SAMPLES    240     /* 30ms at 8000 hz */
59 #define BUFFER_SAMPLES  8000
60
61 static char *tdesc = "iLBC/PCM16 (signed linear) Codec Translator";
62
63 struct ilbc_coder_pvt {
64         iLBC_Enc_Inst_t enc;
65         iLBC_Dec_Inst_t dec;
66         /* Enough to store a full second */
67         int16_t buf[BUFFER_SAMPLES];
68 };
69
70 static int lintoilbc_new(struct ast_trans_pvt *pvt)
71 {
72         struct ilbc_coder_pvt *tmp = pvt->pvt;
73
74         initEncode(&tmp->enc, ILBC_MS);
75
76         return 0;
77 }
78
79 static int ilbctolin_new(struct ast_trans_pvt *pvt)
80 {
81         struct ilbc_coder_pvt *tmp = pvt->pvt;
82
83         initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
84
85         return 0;
86 }
87
88 static struct ast_frame *lintoilbc_sample(void)
89 {
90         static struct ast_frame f;
91         f.frametype = AST_FRAME_VOICE;
92         f.subclass = AST_FORMAT_SLINEAR;
93         f.datalen = sizeof(slin_ilbc_ex);
94         f.samples = sizeof(slin_ilbc_ex)/2;
95         f.mallocd = 0;
96         f.offset = 0;
97         f.src = __PRETTY_FUNCTION__;
98         f.data = slin_ilbc_ex;
99         return &f;
100 }
101
102 static struct ast_frame *ilbctolin_sample(void)
103 {
104         static struct ast_frame f;
105         f.frametype = AST_FRAME_VOICE;
106         f.subclass = AST_FORMAT_ILBC;
107         f.datalen = sizeof(ilbc_slin_ex);
108         /* All frames are 30 ms long */
109         f.samples = ILBC_SAMPLES;
110         f.mallocd = 0;
111         f.offset = 0;
112         f.src = __PRETTY_FUNCTION__;
113         f.data = ilbc_slin_ex;
114         return &f;
115 }
116
117 /*! \brief decode a frame and store in outbuf */
118 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
119 {
120         struct ilbc_coder_pvt *tmp = pvt->pvt;
121         int plc_mode = 1; /* 1 = normal data, 0 = plc */
122         /* Assuming there's space left, decode into the current buffer at
123            the tail location.  Read in as many frames as there are */
124         int x,i;
125         int16_t *dst = (int16_t *)pvt->outbuf;
126         float tmpf[ILBC_SAMPLES];
127
128         if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
129                 f->datalen = ILBC_FRAME_LEN;
130                 f->samples = ILBC_SAMPLES;
131                 plc_mode = 0;   /* do native plc */
132                 pvt->samples += ILBC_SAMPLES;
133         }
134
135         if (f->datalen % ILBC_FRAME_LEN) {
136                 ast_log(LOG_WARNING, "Huh?  An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, f->datalen);
137                 return -1;
138         }
139         
140         for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
141                 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {     
142                         ast_log(LOG_WARNING, "Out of buffer space\n");
143                         return -1;
144                 }               
145                 iLBC_decode(tmpf, plc_mode ? f->data + x : NULL, &tmp->dec, plc_mode);
146                 for ( i=0; i < ILBC_SAMPLES; i++)
147                         dst[pvt->samples + i] = tmpf[i];
148                 pvt->samples += ILBC_SAMPLES;
149                 pvt->datalen += 2*ILBC_SAMPLES;
150         }
151         return 0;
152 }
153
154 /*! \brief store a frame into a temporary buffer, for later decoding */
155 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
156 {
157         struct ilbc_coder_pvt *tmp = pvt->pvt;
158
159         /* Just add the frames to our stream */
160         /* XXX We should look at how old the rest of our stream is, and if it
161            is too old, then we should overwrite it entirely, otherwise we can
162            get artifacts of earlier talk that do not belong */
163         memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
164         pvt->samples += f->samples;
165         return 0;
166 }
167
168 /*! \brief encode the temporary buffer and generate a frame */
169 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
170 {
171         struct ilbc_coder_pvt *tmp = pvt->pvt;
172         int datalen = 0;
173         int samples = 0;
174
175         /* We can't work on anything less than a frame in size */
176         if (pvt->samples < ILBC_SAMPLES)
177                 return NULL;
178         while (pvt->samples >= ILBC_SAMPLES) {
179                 float tmpf[ILBC_SAMPLES];
180                 int i;
181                 /* Encode a frame of data */
182                 for ( i = 0 ; i < ILBC_SAMPLES ; i++ )
183                         tmpf[i] = tmp->buf[i];
184                 iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
185                 datalen += ILBC_FRAME_LEN;
186                 samples += ILBC_SAMPLES;
187                 pvt->samples -= ILBC_SAMPLES;
188                 /* Move the data at the end of the buffer to the front */
189                 if (pvt->samples)
190                         memmove(tmp->buf, tmp->buf + ILBC_SAMPLES, pvt->samples * 2);
191         }
192         return ast_trans_frameout(pvt, datalen, samples);
193 }
194
195 static struct ast_translator ilbctolin = {
196         .name = "ilbctolin", 
197         .srcfmt = AST_FORMAT_ILBC,
198         .dstfmt = AST_FORMAT_SLINEAR,
199         .newpvt = ilbctolin_new,
200         .framein = ilbctolin_framein,
201         .sample = ilbctolin_sample,
202         .desc_size = sizeof(struct ilbc_coder_pvt),
203         .buf_size = BUFFER_SAMPLES * 2,
204 };
205
206 static struct ast_translator lintoilbc = {
207         .name = "lintoilbc", 
208         .srcfmt = AST_FORMAT_SLINEAR,
209         .dstfmt = AST_FORMAT_ILBC,
210         .newpvt = lintoilbc_new,
211         .framein = lintoilbc_framein,
212         .frameout = lintoilbc_frameout,
213         .sample = lintoilbc_sample,
214         .desc_size = sizeof(struct ilbc_coder_pvt),
215         .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
216 };
217
218 static int unload_module(void *mod)
219 {
220         int res;
221         res = ast_unregister_translator(&lintoilbc);
222         res |= ast_unregister_translator(&ilbctolin);
223         return res;
224 }
225
226 static int load_module(void *mod)
227 {
228         int res;
229         res = ast_register_translator(&ilbctolin, mod);
230         if (!res) 
231                 res=ast_register_translator(&lintoilbc, mod);
232         else
233                 ast_unregister_translator(&ilbctolin);
234         return res;
235 }
236
237 static const char *description(void)
238 {
239         return tdesc;
240 }
241
242 static const char *key(void)
243 {
244         return ASTERISK_GPL_KEY;
245 }
246
247 STD_MOD(MOD_1, NULL, NULL, NULL);