SS5: improvements
[lcr.git] / gsm_audio.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** gsm audio                                                                 **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 extern "C" {
13 #include "libgsmfr/inc/gsm.h"
14
15
16 /* create gsm instance */
17 void *gsm_fr_create(void)
18 {
19         int value = 1;
20         gsm handle;
21
22         handle = gsm_create();
23         if (handle)
24                 gsm_option(handle, 0/*GSM_OPT_WAV49*/, &value);
25
26         return handle;
27 }
28
29 /* free gsm instance */
30 void gsm_fr_destroy(void *arg)
31 {
32         gsm_destroy((gsm)arg);
33 }
34
35 /* decode frame into samples, return error */
36 int gsm_fr_decode(void *arg, unsigned char *frame, signed short *samples)
37 {
38 //      int value = 0;
39
40 //      gsm_option((gsm)arg, GSM_OPT_FRAME_INDEX, &value);
41         return gsm_decode((gsm)arg, (gsm_byte *)frame, (gsm_signal *)samples);
42 }
43
44 /* encode samples into frame */
45 void gsm_fr_encode(void *arg, signed short *samples, unsigned char *frame)
46 {
47 //      int value = 0;
48         
49 //      gsm_option((gsm)arg, GSM_OPT_FRAME_INDEX, &value);
50         gsm_encode((gsm)arg, (gsm_signal *)samples, (gsm_byte *)frame);
51 }
52
53 #ifdef WITH_GSMAMR
54
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include <opencore-amrnb/interf_dec.h>
59 #include <opencore-amrnb/interf_enc.h>
60
61
62 struct codec_efr_state {
63         void *encoder;
64         void *decoder;
65 };
66
67 /* create gsm instance */
68 void *gsm_amr_create(void)
69 {
70         struct codec_efr_state *st;
71
72         st = (struct codec_efr_state *)calloc(1, sizeof(*st));
73         if (!st)
74                 return NULL;
75
76         st->encoder = Encoder_Interface_init(0);
77         st->decoder = Decoder_Interface_init();
78
79         return (void *)st;
80 }
81
82 /* free gsm instance */
83 void gsm_amr_destroy(void *arg)
84 {
85         struct codec_efr_state *st = (struct codec_efr_state *)arg;
86
87         Decoder_Interface_exit(st->decoder);
88         Encoder_Interface_exit(st->encoder);
89
90         return;
91 }
92
93 enum Mode amr_mode[8] = {
94         MR475,    /* 4.75 kbps */
95         MR515,    /* 5.15 kbps */
96         MR59,     /* 5.90 kbps */
97         MR67,     /* 6.70 kbps */
98         MR74,     /* 7.40 kbps */
99         MR795,    /* 7.95 kbps */
100         MR102,    /* 10.2 kbps */
101         MR122,    /* 12.2 kbps */
102 };
103
104 /* decode frame into samples, return error */
105 int gsm_amr_decode(void *arg, unsigned char *frame, signed short *samples)
106 {
107         struct codec_efr_state *st = (struct codec_efr_state *)arg;
108
109         Decoder_Interface_Decode(
110                 st->decoder,
111                 (const unsigned char*) frame + 1,
112                 (short *) samples,
113                 0
114         );
115
116         return 0;
117 }
118
119 /* encode samples into frame */
120 int gsm_amr_encode(void *arg, signed short *samples, unsigned char *frame, int mode)
121 {
122         struct codec_efr_state *st = (struct codec_efr_state *)arg;
123         int rv;
124
125         rv = Encoder_Interface_Encode(
126                 st->encoder,
127                 amr_mode[mode],
128                 (const short*) samples,
129                 (unsigned char*) frame + 1,
130                 1
131         );
132
133         frame[0] = 0xf0; /* no request */
134
135         return rv + 1;
136 }
137
138 const unsigned short gsm690_12_2_bitorder[244] = {
139           0,   1,   2,   3,   4,   5,   6,   7,   8,   9,
140          10,  11,  12,  13,  14,  23,  15,  16,  17,  18,
141          19,  20,  21,  22,  24,  25,  26,  27,  28,  38,
142         141,  39, 142,  40, 143,  41, 144,  42, 145,  43,
143         146,  44, 147,  45, 148,  46, 149,  47,  97, 150,
144         200,  48,  98, 151, 201,  49,  99, 152, 202,  86,
145         136, 189, 239,  87, 137, 190, 240,  88, 138, 191,
146         241,  91, 194,  92, 195,  93, 196,  94, 197,  95,
147         198,  29,  30,  31,  32,  33,  34,  35,  50, 100,
148         153, 203,  89, 139, 192, 242,  51, 101, 154, 204,
149          55, 105, 158, 208,  90, 140, 193, 243,  59, 109,
150         162, 212,  63, 113, 166, 216,  67, 117, 170, 220,
151          36,  37,  54,  53,  52,  58,  57,  56,  62,  61,
152          60,  66,  65,  64,  70,  69,  68, 104, 103, 102,
153         108, 107, 106, 112, 111, 110, 116, 115, 114, 120,
154         119, 118, 157, 156, 155, 161, 160, 159, 165, 164,
155         163, 169, 168, 167, 173, 172, 171, 207, 206, 205,
156         211, 210, 209, 215, 214, 213, 219, 218, 217, 223,
157         222, 221,  73,  72,  71,  76,  75,  74,  79,  78,
158          77,  82,  81,  80,  85,  84,  83, 123, 122, 121,
159         126, 125, 124, 129, 128, 127, 132, 131, 130, 135,
160         134, 133, 176, 175, 174, 179, 178, 177, 182, 181,
161         180, 185, 184, 183, 188, 187, 186, 226, 225, 224,
162         229, 228, 227, 232, 231, 230, 235, 234, 233, 238,
163         237, 236,  96, 199,
164 };
165
166 /* decode frame into samples, return error */
167 int gsm_efr_decode(void *arg, unsigned char *frame, signed short *samples)
168 {
169         struct codec_efr_state *st = (struct codec_efr_state *)arg;
170         unsigned char cod[32], bit;
171         int i, si;
172
173         cod[0] = 0x3c; /* good AMR 12,2 frame */
174         memset(cod + 1, 0, 31);
175
176         for (i = 0; i < 244; i++) {
177                 si = gsm690_12_2_bitorder[i] + 4;
178                 bit = (frame[si >> 3] >> (7 - (si & 7))) & 1;
179                 cod[(i >> 3) + 1] |= (bit << (7 - (i & 7)));
180         }
181
182         Decoder_Interface_Decode(
183                 st->decoder,
184                 (const unsigned char*) cod,
185                 (short *) samples,
186                 0
187         );
188
189         return 0;
190 }
191
192 /* encode samples into frame */
193 int gsm_efr_encode(void *arg, signed short *samples, unsigned char *frame)
194 {
195         struct codec_efr_state *st = (struct codec_efr_state *)arg;
196         int rv;
197         unsigned char cod[32], bit;
198         int i, di;
199
200         rv = Encoder_Interface_Encode(
201                 st->encoder,
202                 MR122,
203                 (const short*) samples,
204                 (unsigned char*) cod,
205                 1
206         );
207
208         if (cod[0] != 0x3c)
209                 return -1;
210
211         frame[0] = 0xc0;
212         memset(frame + 1, 0, 30);
213
214         for (i = 0; i < 244; i++) {
215                 di = gsm690_12_2_bitorder[i] + 4;
216                 bit = (cod[(i >> 3) + 1] >> (7 - (i & 7))) & 1;
217                 frame[di >> 3] |= (bit << (7 - (di & 7)));
218         }
219         return rv;
220 }
221
222 #endif
223
224 #ifdef WITH_GSMHR
225
226 #include <gsmhr/gsmhr.h>
227
228 /* create gsm instance */
229 void *gsm_hr_create(void)
230 {
231         struct gsmhr *state;
232         
233         state = gsmhr_init();
234         
235         return state;
236 }
237
238 /* free gsm instance */
239 void gsm_hr_destroy(void *arg)
240 {
241         gsmhr_exit((struct gsmhr *)arg);
242 }
243
244 #include <string.h>
245 /* decode frame into samples, return error */
246 int gsm_hr_decode(void *arg, unsigned char *frame, signed short *samples)
247 {
248         int16_t w[22];
249         int rc;
250
251         w[0] = frame[1] >> 3;
252         w[1] = ((frame[1] & 0x07) << 8) | frame[2];
253         w[2] = (frame[3] << 1) | (frame[4] >> 7);
254         w[3] = ((frame[4] & 0x7f) << 1) | (frame[5] >> 7);
255         w[4] = (frame[5] & 0x7f) >> 6;
256         w[5] = (frame[5] & 0x3f) >> 4;
257         if (w[5]) {
258                 /* voiced */
259                 w[6] = ((frame[5] & 0x0f) << 4) | (frame[6] >> 4);
260                 w[7] = ((frame[6] & 0x0f) << 5) | (frame[7] >> 3);
261                 w[8] = ((frame[7] & 0x07) << 2) | (frame[8] >> 6);
262                 w[9] = (frame[8] & 0x3f) >> 2;
263                 w[10] = ((frame[8] & 0x03) << 7) | (frame[9] >> 1);
264                 w[11] = ((frame[9] & 0x01) << 4) | (frame[10] >> 4);
265                 w[12] = frame[10] & 0x0f;
266                 w[13] = (frame[11] << 1) | (frame[12] >> 7);
267                 w[14] = (frame[12] & 0x7f) >> 2;
268                 w[15] = ((frame[12] & 0x03) << 2) | (frame[13] >> 6);
269                 w[16] = ((frame[13] & 0x3f) << 3) | (frame[14] >> 5);
270                 w[17] = frame[14] & 0x1f;
271         } else {
272                 /* unvoiced */
273                 w[6] = ((frame[5] & 0x0f) << 3) | (frame[6] >> 5);
274                 w[7] = ((frame[6] & 0x1f) << 2) | (frame[7] >> 6);
275                 w[8] = (frame[7] & 0x3f) >> 1;
276                 w[9] = ((frame[7] & 0x01) << 6) | (frame[8] >> 2);
277                 w[10] = ((frame[8] & 0x03) << 5) | (frame[9] >> 3);
278                 w[11] = ((frame[9] & 0x07) << 2) | (frame[10] >> 6);
279                 w[12] = ((frame[10] & 0x3f) << 1) | (frame[11] >> 7);
280                 w[13] = frame[11] & 0x7f;
281                 w[14] = frame[12] >> 3;
282                 w[15] = ((frame[12] & 0x07) << 4) | (frame[13] >> 4);
283                 w[16] = ((frame[13] & 0x1f) << 3) | (frame[14] >> 5);
284                 w[17] = frame[14] & 0x1f;
285         }
286         w[18] = 0;              /* BFI : 1 bit */
287         w[19] = 0;              /* UFI : 1 bit */
288         w[20] = 0;              /* SID : 2 bit */
289         w[21] = 0;              /* TAF : 1 bit */
290
291         rc = gsmhr_decode((struct gsmhr *)arg, samples, w);
292
293         if (rc < 0)
294                 return rc;
295
296         return 0;
297 }
298
299 /* encode samples into frame */
300 void gsm_hr_encode(void *arg, signed short *samples, unsigned char *frame)
301 {
302         int16_t w[22];
303
304         gsmhr_encode((struct gsmhr *)arg, w, samples);
305
306         if (!frame)
307                 return;
308
309         frame[0] = 0x00;
310         frame[1] = (w[0] << 3) | (w[1] >> 8);
311         frame[2] = w[1];
312         frame[3] = (w[2] >> 1);
313         frame[4] = (w[2] << 7) | (w[3] >> 1);
314         frame[5] = (w[3] << 7) | (w[4] << 6) | (w[5] << 4);
315         if (w[5]) {
316                 /* voiced */
317                 frame[5] |= (w[6] >> 4);
318                 frame[6] = (w[6] << 4) | (w[7] >> 5);
319                 frame[7] = (w[7] << 3) | (w[8] >> 2);
320                 frame[8] = (w[8] << 6) | (w[9] << 2) | (w[10] >> 7);
321                 frame[9] = (w[10] << 1) | (w[11] >> 4);
322                 frame[10] = (w[11] << 4) | w[12];
323                 frame[11] = (w[13] >> 1);
324                 frame[12] = (w[13] << 7) | (w[14] << 2) | (w[15] >> 2);
325                 frame[13] = (w[15] << 6) | (w[16] >> 3);
326                 frame[14] = (w[16] << 5) | w[17];
327         } else {
328                 /* unvoiced */
329                 frame[5] |= (w[6] >> 3);
330                 frame[6] = (w[6] << 5) | (w[7] >> 2);
331                 frame[7] = (w[7] << 6) | (w[8] << 1) | (w[9] >> 6);
332                 frame[8] = (w[9] << 2) | (w[10] >> 5);
333                 frame[9] = (w[10] << 3) | (w[11] >> 2);
334                 frame[10] = (w[11] << 6) | (w[12] >> 1);
335                 frame[11] = (w[12] << 7) | w[13];
336                 frame[12] = (w[14] << 3) | (w[15] >> 4);
337                 frame[13] = (w[15] << 4) | (w[16] >> 3);
338                 frame[14] = (w[16] << 5) | w[17];
339         }
340 }
341
342 #endif
343
344 } /* extern "C" */
345