]> git.mxchange.org Git - flightgear.git/blob - 3rdparty/iaxclient/lib/codec_ilbc.c
VS2015 compatability fixes.
[flightgear.git] / 3rdparty / iaxclient / lib / codec_ilbc.c
1 /*
2  * iaxclient: a cross-platform IAX softphone library
3  *
4  * Copyrights:
5  * Copyright (C) 2003-2006, Horizon Wimba, Inc.
6  * Copyright (C) 2007, Wimba, Inc.
7  *
8  * Contributors:
9  * Steve Kann <stevek@stevek.com>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU Lesser (Library) General Public License.
13  */
14
15 #include "codec_ilbc.h"
16 #include "iaxclient_lib.h"
17 #include "iLBC/iLBC_encode.h"
18 #include "iLBC/iLBC_decode.h"
19
20
21 static void destroy ( struct iaxc_audio_codec *c) {
22     free(c->encstate);
23     free(c->decstate);
24     free(c);
25 }
26
27
28 static int decode ( struct iaxc_audio_codec *c,
29     int *inlen, char *in, int *outlen, short *out ) {
30
31     float fbuf[240];
32     int i;
33
34     if(*inlen == 0) {
35         //fprintf(stderr, "ILBC Interpolate\n");
36         iLBC_decode(fbuf, NULL, c->decstate, 0);
37         for(i=0;i<240;i++)
38             out[i] = fbuf[i];
39         *outlen -= 240;
40         return 0;
41     }
42
43
44     /* need to decode minimum of 33 bytes to 160 byte output */
45     if( (*inlen < 50) || (*outlen < 240) ) {
46         fprintf(stderr, "codec_ilbc: inlen = %d outlen= %d\n",*inlen,*outlen);
47         return -1;
48     }
49
50     while( (*inlen >= 50) && (*outlen >= 240) ) {
51         iLBC_decode(fbuf, in, c->decstate, 1);
52         for(i=0;i<240;i++)
53             out[i] = fbuf[i];
54
55         out += 240;
56         *outlen -= 240;
57         in += 50;
58         *inlen -= 50;
59     }
60
61     return 0;
62 }
63
64 static int encode ( struct iaxc_audio_codec *c,
65     int *inlen, short *in, int *outlen, char *out ) {
66
67     float fbuf[240];
68     int i;
69
70     while( (*inlen >= 240) && (*outlen >= 50) ) {
71
72         for(i=0;i<240;i++)
73             fbuf[i] = in[i];
74
75         iLBC_encode(out,fbuf, c->encstate);
76
77         out += 50;
78         *outlen -= 50;
79         in += 240;
80         *inlen -= 240;
81     }
82
83     return 0;
84 }
85
86 struct iaxc_audio_codec *codec_audio_ilbc_new() {
87   struct iaxc_audio_codec *c = calloc(sizeof(struct iaxc_audio_codec),1);
88
89
90   if(!c) return c;
91
92   strcpy(c->name,"iLBC");
93   c->format = IAXC_FORMAT_ILBC;
94   c->encode = encode;
95   c->decode = decode;
96   c->destroy = destroy;
97
98   c->minimum_frame_size = 240;
99
100   c->encstate = calloc(sizeof(iLBC_Enc_Inst_t),1);
101   c->decstate = calloc(sizeof(iLBC_Dec_Inst_t),1);
102
103   /* leaks a bit on no-memory */
104   if(!(c->encstate && c->decstate))
105       return NULL;
106
107   /* the 30 parameters are used for the latest iLBC sources, in
108    * http://www.ietf.org/internet-drafts/draft-ietf-avt-ilbc-codec-05.txt
109    * as used in asterisk-CVS as of 14 Oct 2004 */
110   initEncode(c->encstate, 30);
111   initDecode(c->decstate, 30, 1); /* use enhancer */
112
113   return c;
114 }
115