aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-trx/amr.c
blob: 70d94ca9a41e18865356f895bf59cd951aad6ea5 (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
/* AMR support for OsmoBTS-TRX */

/* (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 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 Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

static int amr_len_by_ft[16] = {
	12, 13, 15, 17, 19, 20, 26, 31,
	0,  0,  0,  0,  0,  0,  0,  0
};

int amr_decompose_payload(uint8_t *payload, int payload_len, uint8_t *_cmr,
	uint8_t *_ft, uint8_t *_bfi)
{
	uint8_t cmr, f, ft, q;

	if (payload_len < 2)
		return -EINVAL;

	cmr = payload[0] >> 4;
	if (_cmr)
		*_cmr = cmr;

	f = payload[1] >> 7;

	ft = (payload[1] >> 3) & 0xf;
	if (_ft)
		*_ft = ft;

	q = (payload[1] >> 2) & 0x1;
	if (_bfi)
		*_bfi = !q;
	
	if (f) {
		fprintf(stderr, "%s: multiple payloads not supported\n",
			__func__);
		return -ENOTSUP;
	}

	if (payload_len - 2 < amr_len_by_ft[ft])
		return -EINVAL;

	return 2 + amr_len_by_ft[ft];
}

int amr_compose_payload(uint8_t *payload, uint8_t cmr, uint8_t ft, uint8_t bfi)
{
	if (cmr >= 16)
		return -EINVAL;

	if (ft >= 16)
		return -EINVAL;

	payload[0] = cmr << 4;

	payload[1] = (ft << 3) | ((!bfi) << 2); /* F = 0 */

	return 2 + amr_len_by_ft[ft];
}