]> git.mxchange.org Git - flightgear.git/blob - 3rdparty/iaxclient/lib/codec_gsm.c
VS2015 compatability fixes.
[flightgear.git] / 3rdparty / iaxclient / lib / codec_gsm.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_gsm.h"
16 #include "iaxclient_lib.h"
17 #include "gsm.h"
18
19 struct state {
20     gsm gsmstate;
21     plc_state_t plc;
22 };
23
24
25 static void destroy ( struct iaxc_audio_codec *c) {
26
27     struct state * encstate = (struct state *) c->encstate;
28     struct state * decstate = (struct state *) c->decstate;
29
30     gsm_destroy(encstate->gsmstate);
31     gsm_destroy(decstate->gsmstate);
32     free(c->encstate);
33     free(c->decstate);
34     free(c);
35 }
36
37
38 static int decode ( struct iaxc_audio_codec *c,
39     int *inlen, unsigned char *in, int *outlen, short *out ) {
40     struct state * decstate = (struct state *) c->decstate;
41
42     /* use generic interpolation */
43     if(*inlen == 0) {
44         int interp_len = 160;
45         if(*outlen < interp_len) interp_len = *outlen;
46         plc_fillin(&decstate->plc,out,interp_len);
47         *outlen -= interp_len;
48         return 0;
49     }
50
51     /* need to decode minimum of 33 bytes to 160 byte output */
52     while( (*inlen >= 33) && (*outlen >= 160) ) {
53         if(gsm_decode(decstate->gsmstate, in, out))
54         {
55             fprintf(stderr, "codec_gsm: gsm_decode returned error\n");
56             return -1;
57         }
58
59         /* push decoded data to interpolation buffer */
60         plc_rx(&decstate->plc,out,160);
61
62         /* we used 33 bytes of input, and 160 bytes of output */
63         *inlen -= 33;
64         in += 33;
65         *outlen -= 160;
66         out += 160;
67     }
68
69     return 0;
70 }
71
72 static int encode ( struct iaxc_audio_codec *c,
73     int *inlen, short *in, int *outlen, unsigned char *out ) {
74
75     struct state * encstate = (struct state *) c->encstate;
76
77
78     /* need to encode minimum of 160 bytes to 33 byte output */
79     while( (*inlen >= 160) && (*outlen >= 33) ) {
80         gsm_encode(encstate->gsmstate, in, out);
81
82         /* we used 160 bytes of input, and 33 bytes of output */
83         *inlen -= 160;
84         in += 160;
85         *outlen -= 33;
86         out += 33;
87     }
88
89     return 0;
90 }
91
92 struct iaxc_audio_codec *codec_audio_gsm_new() {
93
94   struct state * encstate;
95   struct state * decstate;
96   struct iaxc_audio_codec *c = (struct iaxc_audio_codec *)calloc(sizeof(struct iaxc_audio_codec),1);
97
98
99   if(!c) return c;
100
101   strcpy(c->name,"gsm 06.10");
102   c->format = IAXC_FORMAT_GSM;
103   c->encode = encode;
104   c->decode = decode;
105   c->destroy = destroy;
106
107   c->minimum_frame_size = 160;
108
109   c->encstate = calloc(sizeof(struct state),1);
110   c->decstate = calloc(sizeof(struct state),1);
111
112   /* leaks a bit on no-memory */
113   if(!(c->encstate && c->decstate))
114       return NULL;
115
116   encstate = (struct state *) c->encstate;
117   decstate = (struct state *) c->decstate;
118
119   encstate->gsmstate = gsm_create();
120   decstate->gsmstate = gsm_create();
121
122   if(!(encstate->gsmstate && decstate->gsmstate))
123       return NULL;
124
125   return c;
126 }
127