aboutsummaryrefslogtreecommitdiffstats
path: root/src/coding/gsm0503_amr_dtx.c
blob: 7de3b2819d816ea97d06c31e6e9046934f5a559b (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * (C) 2020-2022 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

#include <osmocom/core/bits.h>
#include <osmocom/core/conv.h>
#include <osmocom/core/utils.h>
#include <osmocom/coding/gsm0503_amr_dtx.h>
#include <osmocom/coding/gsm0503_parity.h>
#include <osmocom/gsm/gsm0503.h>

#define S2U(b)	((b) < 0)

/* See also: 3GPP TS 05.03, chapter 3.10.1.3, 3.10.5.2 Identification marker */
static const ubit_t id_marker_1[] = { 1, 0, 1, 1, 0, 0, 0, 0, 1 };

/* See also: 3GPP TS 05.03, chapter 3.9.1.3, 3.10.2.2, 3.10.2.2 Identification marker */
static const ubit_t id_marker_0[] = { 0, 1, 0, 0, 1, 1, 1, 1, 0 };

/* See also: 3GPP TS 05.03, chapter 3.9 Adaptive multi rate speech channel at full rate (TCH/AFS) */
static const ubit_t codec_mode_1_sid[] = { 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0 };
static const ubit_t codec_mode_2_sid[] = { 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0 };
static const ubit_t codec_mode_3_sid[] = { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
static const ubit_t codec_mode_4_sid[] = { 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1 };

const struct value_string gsm0503_amr_dtx_frame_names[] = {
	{ AMR_OTHER,		"AMR_OTHER (audio)" },
	{ AFS_SID_FIRST,	"AFS_SID_FIRST" },
	{ AFS_SID_UPDATE,	"AFS_SID_UPDATE (marker)" },
	{ AFS_SID_UPDATE_CN,	"AFS_SID_UPDATE_CN (audio)" },
	{ AFS_ONSET,		"AFS_ONSET" },
	{ AHS_SID_UPDATE,	"AHS_SID_UPDATE (marker)" },
	{ AHS_SID_UPDATE_CN,	"AHS_SID_UPDATE_CN (audio)" },
	{ AHS_SID_FIRST_P1,	"AHS_SID_FIRST_P1" },
	{ AHS_SID_FIRST_P2,	"AHS_SID_FIRST_P2" },
	{ AHS_ONSET,		"AHS_ONSET" },
	{ AHS_SID_FIRST_INH,	"AHS_SID_FIRST_INH" },
	{ AHS_SID_UPDATE_INH,	"AHS_SID_UPDATE_INH" },
	{ 0, NULL }
};

static bool detect_afs_id_marker(int *n_errors, int *n_bits_total, const sbit_t *sbits, uint8_t offset, uint8_t count,
				 const ubit_t * id_marker, uint8_t id_marker_len)
{
	unsigned int i, k;
	unsigned int id_bit_nr = 0;
	int errors = 0;
	int bits = 0;

	/* Override coded in-band data */
	sbits += offset;

	/* Check for identification marker bits */
	for (i = 0; i < count; i++) {
		for (k = 0; k < 4; k++) {
			if (*sbits == 0 || id_marker[id_bit_nr % id_marker_len] != S2U(*sbits))
				errors++;
			id_bit_nr++;
			sbits++;
			bits++;
		}

		/* Jump to the next block of 4 bits */
		sbits += 4;
	}

	*n_errors = errors;
	*n_bits_total = bits;

	/* Tolerate up to 1/8 errornous bits */
	return *n_errors < *n_bits_total / 8;
}

static bool detect_ahs_id_marker(int *n_errors, int *n_bits_total, const sbit_t *sbits, const ubit_t *id_marker)
{
	unsigned int i, k;
	int errors = 0;
	int bits = 0;

	/* Override coded in-band data */
	sbits += 16;

	/* Check first identification marker bits (23*9 bits) */
	for (i = 0; i < 23; i++) {
		for (k = 0; k < 9; k++) {
			if (*sbits == 0 || id_marker[k] != S2U(*sbits))
				errors++;
			sbits++;
			bits++;
		}
	}

	/* Check remaining identification marker bits (5 bits) */
	for (k = 0; k < 5; k++) {
		if (*sbits == 0 || id_marker[k] != S2U(*sbits))
			errors++;
		sbits++;
		bits++;
	}

	*n_errors = errors;
	*n_bits_total = bits;

	/* Tolerate up to 1/8 errornous bits */
	return *n_errors < *n_bits_total / 8;
}

static bool detect_interleaved_ahs_id_marker(int *n_errors, int *n_bits_total, const sbit_t *sbits, uint8_t offset,
					     uint8_t n_bits, const ubit_t * id_marker, uint8_t id_marker_len)
{
	unsigned int i, k;
	int errors = 0;
	int bits = 0;
	uint8_t full_rounds = n_bits / id_marker_len;
	uint8_t remainder = n_bits % id_marker_len;

	/* Override coded in-band data */
	sbits += offset;

	/* Check first identification marker bits (23*9 bits) */
	for (i = 0; i < full_rounds; i++) {
		for (k = 0; k < id_marker_len; k++) {
			if (*sbits == 0 || id_marker[k] != S2U(*sbits))
				errors++;
			sbits += 2;
			bits++;
		}
	}

	/* Check remaining identification marker bits (5 bits) */
	for (k = 0; k < remainder; k++) {
		if (*sbits == 0 || id_marker[k] != S2U(*sbits))
			errors++;
		sbits += 2;
		bits++;
	}

	*n_errors = errors;
	*n_bits_total = bits;

	/* Tolerate up to 1/8 errornous bits */
	return *n_errors < *n_bits_total / 8;
}

/* Detect an FR AMR SID_FIRST frame by its identifcation marker */
static bool detect_afs_sid_first(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_afs_id_marker(n_errors, n_bits_total, sbits, 32, 53, id_marker_0, 9);
}

/* Detect an FR AMR SID_UPDATE frame by its identification marker */
static bool detect_afs_sid_update(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_afs_id_marker(n_errors, n_bits_total, sbits, 36, 53, id_marker_0, 9);
}

/* Detect an FR AMR ONSET frame by its repeating coded inband data */
static int detect_afs_onset(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	bool rc;

	rc = detect_afs_id_marker(n_errors, n_bits_total, sbits, 4, 57, codec_mode_1_sid, 16);
	if (rc)
		return 0;

	rc = detect_afs_id_marker(n_errors, n_bits_total, sbits, 4, 57, codec_mode_2_sid, 16);
	if (rc)
		return 1;

	rc = detect_afs_id_marker(n_errors, n_bits_total, sbits, 4, 57, codec_mode_3_sid, 16);
	if (rc)
		return 2;

	rc = detect_afs_id_marker(n_errors, n_bits_total, sbits, 4, 57, codec_mode_4_sid, 16);
	if (rc)
		return 3;

	return -1;
}

/* Detect an HR AMR SID UPDATE frame by its identification marker */
static bool detect_ahs_sid_update(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_ahs_id_marker(n_errors, n_bits_total, sbits, id_marker_1);
}

/* Detect an HR AMR SID FIRST (part 1) frame by its identification marker */
static bool detect_ahs_sid_first_p1(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_ahs_id_marker(n_errors, n_bits_total, sbits, id_marker_0);
}

/* Detect an HR AMR SID FIRST (part 2) frame by its repeating coded inband data */
static int detect_ahs_sid_first_p2(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	bool rc;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 0, 114, codec_mode_1_sid, 16);
	if (rc)
		return 0;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 0, 114, codec_mode_2_sid, 16);
	if (rc)
		return 1;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 0, 114, codec_mode_3_sid, 16);
	if (rc)
		return 2;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 0, 114, codec_mode_4_sid, 16);
	if (rc)
		return 3;

	return -1;
}

/* Detect an HR AMR ONSET frame by its repeating coded inband data */
static int detect_ahs_onset(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	bool rc;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 1, 114, codec_mode_1_sid, 16);
	if (rc)
		return 0;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 1, 114, codec_mode_2_sid, 16);
	if (rc)
		return 1;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 1, 114, codec_mode_3_sid, 16);
	if (rc)
		return 2;

	rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 1, 114, codec_mode_4_sid, 16);
	if (rc)
		return 3;

	return -1;
}

/* Detect an HR AMR SID FIRST INHIBIT frame by its identification marker */
static bool detect_ahs_sid_first_inh(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 33, 212, id_marker_1, 9);
}

/* Detect an HR AMR SID UPDATE INHIBIT frame by its identification marker */
static bool detect_ahs_sid_update_inh(int *n_errors, int *n_bits_total, const sbit_t *sbits)
{
	return detect_interleaved_ahs_id_marker(n_errors, n_bits_total, sbits, 33, 212, id_marker_0, 9);
}

/*! Detect FR AMR DTX frame in unmapped, deinterleaved frame bits.
 *  \param[out] n_errors number of errornous bits.
 *  \param[out] n_bits_total number of checked bits.
 *  \param[out] mode_id codec mode ID (0..3 or -1).
 *  \param[in] sbits input soft-bits (456 bit).
 *  \returns dtx frame type. */
enum gsm0503_amr_dtx_frames gsm0503_detect_afs_dtx_frame2(int *n_errors, int *n_bits_total,
							  int *mode_id, const sbit_t *sbits)
{
	if (detect_afs_sid_first(n_errors, n_bits_total, sbits))
		return AFS_SID_FIRST;
	if (detect_afs_sid_update(n_errors, n_bits_total, sbits))
		return AFS_SID_UPDATE;
	if ((*mode_id = detect_afs_onset(n_errors, n_bits_total, sbits)) != -1)
		return AFS_ONSET;

	*n_errors = 0;
	*n_bits_total = 0;
	return AMR_OTHER;
}

/*! Detect FR AMR DTX frame in unmapped, deinterleaved frame bits.
 *  DEPRECATED: use gsm0503_detect_afs_dtx_frame2() instead.
 *  \param[out] n_errors number of errornous bits.
 *  \param[out] n_bits_total number of checked bits.
 *  \param[in] ubits input bits (456 bit).
 *  \returns dtx frame type. */
enum gsm0503_amr_dtx_frames gsm0503_detect_afs_dtx_frame(int *n_errors, int *n_bits_total,
							 const ubit_t *ubits)
{
	int mode_id; /* unused */
	sbit_t sbits[456];

	osmo_ubit2sbit(&sbits[0], &ubits[0], sizeof(sbits));
	return gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &mode_id, &sbits[0]);
}

/*! Detect HR AMR DTX frame in unmapped, deinterleaved frame bits.
 *  \param[out] n_errors number of errornous bits.
 *  \param[out] n_bits_total number of checked bits.
 *  \param[out] mode_id codec ID (CMI or CMC/CMR).
 *  \param[out] mode_id codec mode ID (0..3 or -1).
 *  \param[in] sbits input soft-bits (456 bit).
 *  \returns dtx frame type, */
enum gsm0503_amr_dtx_frames gsm0503_detect_ahs_dtx_frame2(int *n_errors, int *n_bits_total,
							  int *mode_id, const sbit_t *sbits)
{
	if (detect_ahs_sid_update(n_errors, n_bits_total, sbits))
		return AHS_SID_UPDATE;
	if (detect_ahs_sid_first_inh(n_errors, n_bits_total, sbits))
		return AHS_SID_FIRST_INH;
	if (detect_ahs_sid_update_inh(n_errors, n_bits_total, sbits))
		return AHS_SID_UPDATE_INH;
	if (detect_ahs_sid_first_p1(n_errors, n_bits_total, sbits))
		return AHS_SID_FIRST_P1;
	if ((*mode_id = detect_ahs_sid_first_p2(n_errors, n_bits_total, sbits)) != -1)
		return AHS_SID_FIRST_P2;
	if ((*mode_id = detect_ahs_onset(n_errors, n_bits_total, sbits)) != -1)
		return AHS_ONSET;

	*n_errors = 0;
	*n_bits_total = 0;
	return AMR_OTHER;
}

/*! Detect HR AMR DTX frame in unmapped, deinterleaved frame bits.
 *  DEPRECATED: use gsm0503_detect_ahs_dtx_frame2() instead.
 *  \param[out] n_errors number of errornous bits.
 *  \param[out] n_bits_total number of checked bits.
 *  \param[in] ubits input bits (456 bit).
 *  \returns dtx frame type, */
enum gsm0503_amr_dtx_frames gsm0503_detect_ahs_dtx_frame(int *n_errors, int *n_bits_total,
							 const ubit_t *ubits)
{
	int mode_id; /* unused */
	sbit_t sbits[456];

	osmo_ubit2sbit(&sbits[0], &ubits[0], sizeof(sbits));
	return gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &mode_id, &sbits[0]);
}