aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-trx/tch_fr.c
blob: 75f54752785074c534e7df5d9cdfeb7cb2b5edf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * tch_fr.c
 *
 * Copyright (c) 2013  Andreas Eversberg <jolly@eversberg.eu>
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <osmocom/core/bits.h>
#include <osmocom/core/conv.h>
#include <osmocom/core/crcgen.h>

#include "xcch.h"
#include "tch_fr.h"


/*
 * GSM TCH FR/EFR parity
 *
 * g(x) = x^3 + x + 1
 */

const struct osmo_crc8gen_code tch_fr_crc3 = {
	.bits = 3,
	.poly = 0x2,
	.init = 0x0,
	.remainder = 0x7,
};


/*
 * GSM TCH FR/EFR convolutional coding
 *
 * G_0 = 1 + x^3 + x^4
 * G_1 = 1 + x + x^3 + x^4
 */

static const uint8_t conv_tch_fr_next_output[][2] = {
	{ 0, 3 }, { 1, 2 }, { 0, 3 }, { 1, 2 },
	{ 3, 0 }, { 2, 1 }, { 3, 0 }, { 2, 1 },
	{ 3, 0 }, { 2, 1 }, { 3, 0 }, { 2, 1 },
	{ 0, 3 }, { 1, 2 }, { 0, 3 }, { 1, 2 },
};

static const uint8_t conv_tch_fr_next_state[][2] = {
	{  0,  1 }, {  2,  3 }, {  4,  5 }, {  6,  7 },
	{  8,  9 }, { 10, 11 }, { 12, 13 }, { 14, 15 },
	{  0,  1 }, {  2,  3 }, {  4,  5 }, {  6,  7 },
	{  8,  9 }, { 10, 11 }, { 12, 13 }, { 14, 15 },
};

static const struct osmo_conv_code conv_tch_fr = {
	.N = 2,
	.K = 5,
	.len = 185,
	.next_output = conv_tch_fr_next_output,
	.next_state  = conv_tch_fr_next_state,
};


/*
 * GSM TCH FR/EFR interleaving and burst mapping
 *
 * Interleaving:
 *
 * Given 456 coded input bits, form 8 blocks of 114 bits,
 * where event bits of the first 4 block and off bits of the last 4 block
 * are used:
 *
 *      i(B, j) = c(n, k)       k = 0, ..., 455
 *                              n = 0, ..., N, N + 1, ...
 *                              B = B_0 + 4n + (k mod 8)
 *                              j = 2(49k mod 57) + ((k mod 8) div 4)
 *
 * Mapping on Burst:
 *
 *      e(B, j) = i(B, j)
 *      e(B, 59 + j) = i(B, 57 + j)     j = 0, ..., 56
 *      e(B, 57) = h_l(B)
 *      e(B, 58) = h_n(B)
 *
 * Where hl(B) and hn(B) are bits in burst B indicating flags.
 */

static void
tch_fr_deinterleave(sbit_t *cB, sbit_t *iB)
{
	int j, k, B;

	for (k=0; k<456; k++) {
		B = k & 7;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		cB[k] = iB[B * 114 + j];
	}
}

static void
tch_fr_interleave(ubit_t *cB, ubit_t *iB)
{
	int j, k, B;

	for (k=0; k<456; k++) {
		B = k & 7;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		iB[B * 114 + j] = cB[k];
	}
}

static void
tch_fr_burst_unmap(sbit_t *iB, sbit_t *eB, sbit_t *h, int odd)
{
	int i;

	/* brainfuck: only copy even or odd bits */
	for (i=odd; i<57; i+=2)
		iB[i] = eB[i];
	for (i=58-odd; i<114; i+=2)
		iB[i] = eB[i+2];

	if (h && !odd)
		*h = eB[57];

	if (h && odd)
		*h = eB[58];
}

static void
tch_fr_burst_map(ubit_t *iB, ubit_t *eB, ubit_t *h, int odd)
{
	int i;

	/* brainfuck: only copy even or odd bits */
	for (i=odd; i<57; i+=2)
		eB[i] = iB[i];
	for (i=58-odd; i<114; i+=2)
		eB[i+2] = iB[i];

	if (h && !odd)
		eB[57] = *h;
	if (h && odd)
		eB[58] = *h;
}

/* this corresponds to the bit-lengths of the individual codec
 * parameters as indicated in Table 1.1 of TS 06.10 */
static const uint8_t gsm_fr_map[] = {
	6, 6, 5, 5, 4, 4, 3, 3,
	7, 2, 2, 6, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 7, 2, 2, 6, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 7, 2, 2, 6, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 7, 2, 2, 6, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3
};

static void
tch_fr_reassemble(uint8_t *tch_data, ubit_t *d_bits)
{
	int i, j, k, l, o;

	tch_data[0] = 0xd << 4;
	/* reassemble d-bits */
	i = 0; /* counts bits */
	j = 4; /* counts output bits */
	k = gsm_fr_map[0]-1; /* current number bit in element */
	l = 0; /* counts element bits */
	o = 0; /* offset input bits */
	while (i < 260) {
		tch_data[j>>3] |= (d_bits[k+o] << (7-(j&7)));
		if (--k < 0) {
			o += gsm_fr_map[l];
			k = gsm_fr_map[++l]-1;
		}
		i++;
		j++;
	}
}

static void
tch_fr_disassemble(ubit_t *d_bits, uint8_t *tch_data)
{
	int i, j, k, l, o;

	i = 0; /* counts bits */
	j = 4; /* counts input bits */
	k = gsm_fr_map[0]-1; /* current number bit in element */
	l = 0; /* counts element bits */
	o = 0; /* offset output bits */
	while (i < 260) {
		d_bits[k+o] = (tch_data[j>>3] >> (7-(j&7))) & 1;
		if (--k < 0) {
			o += gsm_fr_map[l];
			k = gsm_fr_map[++l]-1;
		}
		i++;
		j++;
	}
}

static void
tch_fr_unreorder(ubit_t *d, ubit_t *p, ubit_t *u)
{
	int i;

	for (i=0; i<91; i++) {
		d[i<<1] = u[i];
		d[(i<<1)+1] = u[184-i];
	}
	for (i=0; i<3; i++)
		p[i] = u[91+i];
}

static void
tch_fr_reorder(ubit_t *u, ubit_t *d, ubit_t *p)
{
	int i;

	for (i=0; i<91; i++) {
		u[i] = d[i<<1];
		u[184-i] = d[(i<<1)+1];
	}
	for (i=0; i<3; i++)
		u[91+i] = p[i];
}

int
tch_fr_decode(uint8_t *tch_data, sbit_t *bursts)
{
	sbit_t iB[912], cB[456], h;
	ubit_t conv[185], d[260], p[3];
	int i, rv, len, steal = 0;

	for (i=0; i<8; i++) {
		tch_fr_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i>>2);
		if (h < 0)
			steal++;
	}

	tch_fr_deinterleave(cB, iB);

	if (steal < 4) {
		osmo_conv_decode(&conv_tch_fr, cB, conv);

		tch_fr_unreorder(d, p, conv);

		for (i=0; i<78; i++)
			d[i+182] = (cB[i+378] < 0) ? 1:0;

		rv = osmo_crc8gen_check_bits(&tch_fr_crc3, d, 50, p);
		if (rv)
			return -1;

		tch_fr_reassemble(tch_data, d);

		len = 33;
	} else {
		rv = xcch_decode_cB(tch_data, cB);
		if (rv)
			return -1;

		len = 23;
	}

	return len;
}

int
tch_fr_encode(ubit_t *bursts, uint8_t *tch_data, int len)
{
	ubit_t iB[912], cB[456], h;
	ubit_t conv[185], d[260], p[3];
	int i;

	switch (len) {
	case 33: /* TCH FR */
		tch_fr_disassemble(d, tch_data);

		osmo_crc8gen_set_bits(&tch_fr_crc3, d, 50, p);

		tch_fr_reorder(conv, d, p);

		memcpy(cB+378, d+182, 78);

		osmo_conv_encode(&conv_tch_fr, conv, cB);

		h = 0;

		break;
	case 23: /* FACCH */
		xcch_encode_cB(cB, tch_data);

		h = 1;

		break;
	default:
		return -1;
	}

	tch_fr_interleave(cB, iB);

	for (i=0; i<8; i++)
		tch_fr_burst_map(&iB[i * 114], &bursts[i * 116], &h, i>>2);

	return 0;
}