1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #include "libgsmfr/inc/gsm.h"
16 /* create gsm instance */
17 void *gsm_fr_create(void)
22 handle = gsm_create();
24 gsm_option(handle, 0/*GSM_OPT_WAV49*/, &value);
29 /* free gsm instance */
30 void gsm_fr_destroy(void *arg)
32 gsm_destroy((gsm)arg);
35 /* decode frame into samples, return error */
36 int gsm_fr_decode(void *arg, unsigned char *frame, signed short *samples)
40 // gsm_option((gsm)arg, GSM_OPT_FRAME_INDEX, &value);
41 return gsm_decode((gsm)arg, (gsm_byte *)frame, (gsm_signal *)samples);
44 /* encode samples into frame */
45 void gsm_fr_encode(void *arg, signed short *samples, unsigned char *frame)
49 // gsm_option((gsm)arg, GSM_OPT_FRAME_INDEX, &value);
50 gsm_encode((gsm)arg, (gsm_signal *)samples, (gsm_byte *)frame);
58 #include <opencore-amrnb/interf_dec.h>
59 #include <opencore-amrnb/interf_enc.h>
62 struct codec_efr_state {
67 /* create gsm instance */
68 void *gsm_amr_create(void)
70 struct codec_efr_state *st;
72 st = (struct codec_efr_state *)calloc(1, sizeof(*st));
76 st->encoder = Encoder_Interface_init(0);
77 st->decoder = Decoder_Interface_init();
82 /* free gsm instance */
83 void gsm_amr_destroy(void *arg)
85 struct codec_efr_state *st = (struct codec_efr_state *)arg;
87 Decoder_Interface_exit(st->decoder);
88 Encoder_Interface_exit(st->encoder);
93 enum Mode amr_mode[8] = {
94 MR475, /* 4.75 kbps */
95 MR515, /* 5.15 kbps */
99 MR795, /* 7.95 kbps */
100 MR102, /* 10.2 kbps */
101 MR122, /* 12.2 kbps */
104 /* decode frame into samples, return error */
105 int gsm_amr_decode(void *arg, unsigned char *frame, signed short *samples)
107 struct codec_efr_state *st = (struct codec_efr_state *)arg;
109 Decoder_Interface_Decode(
111 (const unsigned char*) frame + 1,
119 /* encode samples into frame */
120 int gsm_amr_encode(void *arg, signed short *samples, unsigned char *frame, int mode)
122 struct codec_efr_state *st = (struct codec_efr_state *)arg;
125 rv = Encoder_Interface_Encode(
128 (const short*) samples,
129 (unsigned char*) frame + 1,
133 frame[0] = 0xf0; /* no request */
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,
166 /* decode frame into samples, return error */
167 int gsm_efr_decode(void *arg, unsigned char *frame, signed short *samples)
169 struct codec_efr_state *st = (struct codec_efr_state *)arg;
170 unsigned char cod[32], bit;
173 cod[0] = 0x3c; /* good AMR 12,2 frame */
174 memset(cod + 1, 0, 31);
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)));
182 Decoder_Interface_Decode(
184 (const unsigned char*) cod,
192 /* encode samples into frame */
193 int gsm_efr_encode(void *arg, signed short *samples, unsigned char *frame)
195 struct codec_efr_state *st = (struct codec_efr_state *)arg;
197 unsigned char cod[32], bit;
200 rv = Encoder_Interface_Encode(
203 (const short*) samples,
204 (unsigned char*) cod,
212 memset(frame + 1, 0, 30);
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)));
226 #include <gsmhr/gsmhr.h>
228 /* create gsm instance */
229 void *gsm_hr_create(void)
233 state = gsmhr_init();
238 /* free gsm instance */
239 void gsm_hr_destroy(void *arg)
241 gsmhr_exit((struct gsmhr *)arg);
245 /* decode frame into samples, return error */
246 int gsm_hr_decode(void *arg, unsigned char *frame, signed short *samples)
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;
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;
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;
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 */
291 rc = gsmhr_decode((struct gsmhr *)arg, samples, w);
299 /* encode samples into frame */
300 void gsm_hr_encode(void *arg, signed short *samples, unsigned char *frame)
304 gsmhr_encode((struct gsmhr *)arg, w, samples);
310 frame[1] = (w[0] << 3) | (w[1] >> 8);
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);
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];
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];