blob: 3b2e6694853ed53113c7b09eba7a6fa2d09cb935 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm690.c
2 * GSM 06.90 - GSM AMR Codec. */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +02003/*
4 * (C) 2010 Sylvain Munaut <tnt@246tNt.com>
Harald Welte5785a4a2020-05-13 23:10:31 +02005 * (C) 2020 Harald Welte <laforge@gnumonks.org>
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +02006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020021 */
22
23#include <stdint.h>
Max92db1502016-05-25 18:13:51 +020024#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020027
Max92db1502016-05-25 18:13:51 +020028#include <osmocom/core/utils.h>
Harald Welte5785a4a2020-05-13 23:10:31 +020029#include <osmocom/core/bits.h>
Max92db1502016-05-25 18:13:51 +020030#include <osmocom/codec/codec.h>
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020031/*
32 * These table map between the raw encoder parameter output and
33 * the format used before channel coding. Both in GSM and in various
34 * file/network format (same tables used in several specs).
35 */
36
37/* AMR 12.2 kbits - subjective importance bit ordering */
38 /* This array encodes GSM 05.03 Table 7
39 * It's also TS 26.101 Table B.8
40 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -070041const uint16_t gsm690_12_2_bitorder[244] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020042 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
43 10, 11, 12, 13, 14, 23, 15, 16, 17, 18,
44 19, 20, 21, 22, 24, 25, 26, 27, 28, 38,
45 141, 39, 142, 40, 143, 41, 144, 42, 145, 43,
46 146, 44, 147, 45, 148, 46, 149, 47, 97, 150,
47 200, 48, 98, 151, 201, 49, 99, 152, 202, 86,
48 136, 189, 239, 87, 137, 190, 240, 88, 138, 191,
49 241, 91, 194, 92, 195, 93, 196, 94, 197, 95,
50 198, 29, 30, 31, 32, 33, 34, 35, 50, 100,
51 153, 203, 89, 139, 192, 242, 51, 101, 154, 204,
52 55, 105, 158, 208, 90, 140, 193, 243, 59, 109,
53 162, 212, 63, 113, 166, 216, 67, 117, 170, 220,
54 36, 37, 54, 53, 52, 58, 57, 56, 62, 61,
55 60, 66, 65, 64, 70, 69, 68, 104, 103, 102,
56 108, 107, 106, 112, 111, 110, 116, 115, 114, 120,
57 119, 118, 157, 156, 155, 161, 160, 159, 165, 164,
58 163, 169, 168, 167, 173, 172, 171, 207, 206, 205,
59 211, 210, 209, 215, 214, 213, 219, 218, 217, 223,
60 222, 221, 73, 72, 71, 76, 75, 74, 79, 78,
61 77, 82, 81, 80, 85, 84, 83, 123, 122, 121,
62 126, 125, 124, 129, 128, 127, 132, 131, 130, 135,
63 134, 133, 176, 175, 174, 179, 178, 177, 182, 181,
64 180, 185, 184, 183, 188, 187, 186, 226, 225, 224,
65 229, 228, 227, 232, 231, 230, 235, 234, 233, 238,
66 237, 236, 96, 199,
67};
68
69/* AMR 10.2 kbits - subjective importance bit ordering */
70 /* This array encodes GSM 05.03 Table 8
71 * It's also TS 26.101 Table B.7
72 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -070073const uint16_t gsm690_10_2_bitorder[204] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020074 7, 6, 5, 4, 3, 2, 1, 0, 16, 15,
75 14, 13, 12, 11, 10, 9, 8, 26, 27, 28,
76 29, 30, 31, 115, 116, 117, 118, 119, 120, 72,
77 73, 161, 162, 65, 68, 69, 108, 111, 112, 154,
78 157, 158, 197, 200, 201, 32, 33, 121, 122, 74,
79 75, 163, 164, 66, 109, 155, 198, 19, 23, 21,
80 22, 18, 17, 20, 24, 25, 37, 36, 35, 34,
81 80, 79, 78, 77, 126, 125, 124, 123, 169, 168,
82 167, 166, 70, 67, 71, 113, 110, 114, 159, 156,
83 160, 202, 199, 203, 76, 165, 81, 82, 92, 91,
84 93, 83, 95, 85, 84, 94, 101, 102, 96, 104,
85 86, 103, 87, 97, 127, 128, 138, 137, 139, 129,
86 141, 131, 130, 140, 147, 148, 142, 150, 132, 149,
87 133, 143, 170, 171, 181, 180, 182, 172, 184, 174,
88 173, 183, 190, 191, 185, 193, 175, 192, 176, 186,
89 38, 39, 49, 48, 50, 40, 52, 42, 41, 51,
90 58, 59, 53, 61, 43, 60, 44, 54, 194, 179,
91 189, 196, 177, 195, 178, 187, 188, 151, 136, 146,
92 153, 134, 152, 135, 144, 145, 105, 90, 100, 107,
93 88, 106, 89, 98, 99, 62, 47, 57, 64, 45,
94 63, 46, 55, 56,
95};
96
97/* AMR 7.95 kbits - subjective importance bit ordering */
98 /* This array encodes GSM 05.03 Table 9
99 * It's also TS 26.101 Table B.6
100 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700101const uint16_t gsm690_7_95_bitorder[159] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200102 8, 7, 6, 5, 4, 3, 2, 14, 16, 9,
103 10, 12, 13, 15, 11, 17, 20, 22, 24, 23,
104 19, 18, 21, 56, 88, 122, 154, 57, 89, 123,
105 155, 58, 90, 124, 156, 52, 84, 118, 150, 53,
106 85, 119, 151, 27, 93, 28, 94, 29, 95, 30,
107 96, 31, 97, 61, 127, 62, 128, 63, 129, 59,
108 91, 125, 157, 32, 98, 64, 130, 1, 0, 25,
109 26, 33, 99, 34, 100, 65, 131, 66, 132, 54,
110 86, 120, 152, 60, 92, 126, 158, 55, 87, 121,
111 153, 117, 116, 115, 46, 78, 112, 144, 43, 75,
112 109, 141, 40, 72, 106, 138, 36, 68, 102, 134,
113 114, 149, 148, 147, 146, 83, 82, 81, 80, 51,
114 50, 49, 48, 47, 45, 44, 42, 39, 35, 79,
115 77, 76, 74, 71, 67, 113, 111, 110, 108, 105,
116 101, 145, 143, 142, 140, 137, 133, 41, 73, 107,
117 139, 37, 69, 103, 135, 38, 70, 104, 136,
118};
119
120/* AMR 7.4 kbits - subjective importance bit ordering */
121 /* This array encodes GSM 05.03 Table 10
122 * It's also TS 26.101 Table B.5
123 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700124const uint16_t gsm690_7_4_bitorder[148] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200125 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
126 10, 11, 12, 13, 14, 15, 16, 26, 87, 27,
127 88, 28, 89, 29, 90, 30, 91, 51, 80, 112,
128 141, 52, 81, 113, 142, 54, 83, 115, 144, 55,
129 84, 116, 145, 58, 119, 59, 120, 21, 22, 23,
130 17, 18, 19, 31, 60, 92, 121, 56, 85, 117,
131 146, 20, 24, 25, 50, 79, 111, 140, 57, 86,
132 118, 147, 49, 78, 110, 139, 48, 77, 53, 82,
133 114, 143, 109, 138, 47, 76, 108, 137, 32, 33,
134 61, 62, 93, 94, 122, 123, 41, 42, 43, 44,
135 45, 46, 70, 71, 72, 73, 74, 75, 102, 103,
136 104, 105, 106, 107, 131, 132, 133, 134, 135, 136,
137 34, 63, 95, 124, 35, 64, 96, 125, 36, 65,
138 97, 126, 37, 66, 98, 127, 38, 67, 99, 128,
139 39, 68, 100, 129, 40, 69, 101, 130,
140};
141
142/* AMR 6.7 kbits - subjective importance bit ordering */
143 /* This array encodes GSM 05.03 Table 11
144 * It's also TS 26.101 Table B.4
145 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700146const uint16_t gsm690_6_7_bitorder[134] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200147 0, 1, 4, 3, 5, 6, 13, 7, 2, 8,
148 9, 11, 15, 12, 14, 10, 28, 82, 29, 83,
149 27, 81, 26, 80, 30, 84, 16, 55, 109, 56,
150 110, 31, 85, 57, 111, 48, 73, 102, 127, 32,
151 86, 51, 76, 105, 130, 52, 77, 106, 131, 58,
152 112, 33, 87, 19, 23, 53, 78, 107, 132, 21,
153 22, 18, 17, 20, 24, 25, 50, 75, 104, 129,
154 47, 72, 101, 126, 54, 79, 108, 133, 46, 71,
155 100, 125, 128, 103, 74, 49, 45, 70, 99, 124,
156 42, 67, 96, 121, 39, 64, 93, 118, 38, 63,
157 92, 117, 35, 60, 89, 114, 34, 59, 88, 113,
158 44, 69, 98, 123, 43, 68, 97, 122, 41, 66,
159 95, 120, 40, 65, 94, 119, 37, 62, 91, 116,
160 36, 61, 90, 115,
161};
162
163/* AMR 5.9 kbits - subjective importance bit ordering */
164 /* This array encodes GSM 05.03 Table 12
165 * It's also TS 26.101 Table B.3
166 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700167const uint16_t gsm690_5_9_bitorder[118] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200168 0, 1, 4, 5, 3, 6, 7, 2, 13, 15,
169 8, 9, 11, 12, 14, 10, 16, 28, 74, 29,
170 75, 27, 73, 26, 72, 30, 76, 51, 97, 50,
171 71, 96, 117, 31, 77, 52, 98, 49, 70, 95,
172 116, 53, 99, 32, 78, 33, 79, 48, 69, 94,
173 115, 47, 68, 93, 114, 46, 67, 92, 113, 19,
174 21, 23, 22, 18, 17, 20, 24, 111, 43, 89,
175 110, 64, 65, 44, 90, 25, 45, 66, 91, 112,
176 54, 100, 40, 61, 86, 107, 39, 60, 85, 106,
177 36, 57, 82, 103, 35, 56, 81, 102, 34, 55,
178 80, 101, 42, 63, 88, 109, 41, 62, 87, 108,
179 38, 59, 84, 105, 37, 58, 83, 104,
180};
181
182/* AMR 5.15 kbits - subjective importance bit ordering */
183 /* This array encodes GSM 05.03 Table 13
184 * It's also TS 26.101 Table B.2
185 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700186const uint16_t gsm690_5_15_bitorder[103] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200187 7, 6, 5, 4, 3, 2, 1, 0, 15, 14,
188 13, 12, 11, 10, 9, 8, 23, 24, 25, 26,
189 27, 46, 65, 84, 45, 44, 43, 64, 63, 62,
190 83, 82, 81, 102, 101, 100, 42, 61, 80, 99,
191 28, 47, 66, 85, 18, 41, 60, 79, 98, 29,
192 48, 67, 17, 20, 22, 40, 59, 78, 97, 21,
193 30, 49, 68, 86, 19, 16, 87, 39, 38, 58,
194 57, 77, 35, 54, 73, 92, 76, 96, 95, 36,
195 55, 74, 93, 32, 51, 33, 52, 70, 71, 89,
196 90, 31, 50, 69, 88, 37, 56, 75, 94, 34,
197 53, 72, 91,
198};
199
200/* AMR 4.75 kbits - subjective importance bit ordering */
201 /* This array encodes GSM 05.03 Table 14
202 * It's also TS 26.101 Table B.1
203 */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700204const uint16_t gsm690_4_75_bitorder[95] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200205 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
206 10, 11, 12, 13, 14, 15, 23, 24, 25, 26,
207 27, 28, 48, 49, 61, 62, 82, 83, 47, 46,
208 45, 44, 81, 80, 79, 78, 17, 18, 20, 22,
209 77, 76, 75, 74, 29, 30, 43, 42, 41, 40,
210 38, 39, 16, 19, 21, 50, 51, 59, 60, 63,
211 64, 72, 73, 84, 85, 93, 94, 32, 33, 35,
212 36, 53, 54, 56, 57, 66, 67, 69, 70, 87,
213 88, 90, 91, 34, 55, 68, 89, 37, 58, 71,
214 92, 31, 52, 65, 86,
215};
Max92db1502016-05-25 18:13:51 +0200216
Harald Welte5785a4a2020-05-13 23:10:31 +0200217/*! These constants refer to the length of one "AMR core frame" as per
218 * TS 26.101 Section 4.2.2 / Table 2. */
219const uint8_t gsm690_bitlength[AMR_NO_DATA+1] = {
220 [AMR_4_75] = 95,
221 [AMR_5_15] = 103,
222 [AMR_5_90] = 118,
223 [AMR_6_70] = 134,
224 [AMR_7_40] = 148,
225 [AMR_7_95] = 159,
226 [AMR_10_2] = 204,
227 [AMR_12_2] = 244,
228 [AMR_SID] = 39,
229};
230
231struct ts26101_reorder_table {
232 /*! Table as per TS 26.101 Annex B to compute d-bits from s-bits */
233 const uint16_t *s_to_d;
234 /*! size of table */
235 uint8_t len;
236};
237
238static const struct ts26101_reorder_table ts26101_reorder_tables[8] = {
239 [AMR_4_75] = {
240 .s_to_d = gsm690_4_75_bitorder,
241 .len = ARRAY_SIZE(gsm690_4_75_bitorder),
242 },
243 [AMR_5_15] = {
244 .s_to_d = gsm690_5_15_bitorder,
245 .len = ARRAY_SIZE(gsm690_5_15_bitorder),
246 },
247 [AMR_5_90] = {
248 .s_to_d = gsm690_5_9_bitorder,
249 .len = ARRAY_SIZE(gsm690_5_9_bitorder),
250 },
251 [AMR_6_70] = {
252 .s_to_d = gsm690_6_7_bitorder,
253 .len = ARRAY_SIZE(gsm690_6_7_bitorder),
254 },
255 [AMR_7_40] = {
256 .s_to_d = gsm690_7_4_bitorder,
257 .len = ARRAY_SIZE(gsm690_7_4_bitorder),
258 },
259 [AMR_7_95] = {
260 .s_to_d = gsm690_7_95_bitorder,
261 .len = ARRAY_SIZE(gsm690_7_95_bitorder),
262 },
263 [AMR_10_2] = {
264 .s_to_d = gsm690_10_2_bitorder,
265 .len = ARRAY_SIZE(gsm690_10_2_bitorder),
266 },
267 [AMR_12_2] = {
268 .s_to_d = gsm690_12_2_bitorder,
269 .len = ARRAY_SIZE(gsm690_12_2_bitorder),
270 },
271};
272
273/*! Convert from S-bits (codec output) to d-bits.
274 * \param[out] out user-provided output buffer for generated unpacked d-bits
275 * \param[in] in input buffer for unpacked s-bits
276 * \param[in] n_bits number of bits (in both in and out)
277 * \param[in] AMR mode (0..7) */
278int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode)
279{
280 const struct ts26101_reorder_table *tbl;
281 int i;
282
283 if (amr_mode >= ARRAY_SIZE(ts26101_reorder_tables))
284 return -ENODEV;
285
286 tbl = &ts26101_reorder_tables[amr_mode];
287
288 if (n_bits > tbl->len)
289 return -EINVAL;
290
291 for (i = 0; i < n_bits; i++) {
292 uint16_t n = tbl->s_to_d[i];
293 out[i] = in[n];
294 }
295
296 return n_bits;
297}
298
299/*! Convert from d-bits to s-bits (codec input).
300 * \param[out] out user-provided output buffer for generated unpacked s-bits
301 * \param[in] in input buffer for unpacked d-bits
302 * \param[in] n_bits number of bits (in both in and out)
303 * \param[in] AMR mode (0..7) */
304int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode)
305{
306 const struct ts26101_reorder_table *tbl;
307 int i;
308
309 if (amr_mode >= ARRAY_SIZE(ts26101_reorder_tables))
310 return -ENODEV;
311
312 tbl = &ts26101_reorder_tables[amr_mode];
313
314 if (n_bits > tbl->len)
315 return -EINVAL;
316
317 for (i = 0; i < n_bits; i++) {
318 uint16_t n = tbl->s_to_d[i];
319 out[n] = in[i];
320 }
321
322 return n_bits;
323}
324
Philipp Maier3a5917b2020-03-12 13:22:53 +0100325/* See also RFC 4867 §3.6, Table 1, Column "Total speech bits" */
Max92db1502016-05-25 18:13:51 +0200326static const uint8_t amr_len_by_ft[16] = {
Philipp Maier3a5917b2020-03-12 13:22:53 +0100327 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
Max92db1502016-05-25 18:13:51 +0200328};
329
330const struct value_string osmo_amr_type_names[] = {
331 { AMR_4_75, "AMR 4,75 kbits/s" },
332 { AMR_5_15, "AMR 5,15 kbit/s" },
333 { AMR_5_90, "AMR 5,90 kbit/s" },
334 { AMR_6_70, "AMR 6,70 kbit/s (PDC-EFR)" },
335 { AMR_7_40, "AMR 7,40 kbit/s (TDMA-EFR)" },
336 { AMR_7_95, "AMR 7,95 kbit/s" },
337 { AMR_10_2, "AMR 10,2 kbit/s" },
338 { AMR_12_2, "AMR 12,2 kbit/s (GSM-EFR)" },
339 { AMR_SID, "AMR SID" },
340 { AMR_GSM_EFR_SID, "GSM-EFR SID" },
341 { AMR_TDMA_EFR_SID, "TDMA-EFR SID" },
342 { AMR_PDC_EFR_SID, "PDC-EFR SID" },
343 { AMR_NO_DATA, "No Data/NA" },
344 { 0, NULL },
345};
346
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200347/*! Decode various AMR parameters from RTP payload (RFC 4867) acording to
Max92db1502016-05-25 18:13:51 +0200348 * 3GPP TS 26.101
349 * \param[in] rtppayload Payload from RTP packet
350 * \param[in] payload_len length of rtppayload
351 * \param[out] cmr AMR Codec Mode Request, not filled if NULL
352 * \param[out] cmi AMR Codec Mode Indicator, -1 if not applicable for this type,
353 * not filled if NULL
354 * \param[out] ft AMR Frame Type, not filled if NULL
355 * \param[out] bfi AMR Bad Frame Indicator, not filled if NULL
356 * \param[out] sti AMR SID Type Indicator, -1 if not applicable for this type,
357 * not filled if NULL
358 * \returns length of AMR data or negative value on error
359 */
360int osmo_amr_rtp_dec(const uint8_t *rtppayload, int payload_len, uint8_t *cmr,
361 int8_t *cmi, enum osmo_amr_type *ft,
362 enum osmo_amr_quality *bfi, int8_t *sti)
363{
Maxe9e5f8e2016-11-07 14:49:13 +0100364 if (payload_len < 2 || !rtppayload)
365 return -EINVAL;
366
Max92db1502016-05-25 18:13:51 +0200367 /* RFC 4867 § 4.4.2 ToC - compound payloads are not supported: F = 0 */
368 uint8_t type = (rtppayload[1] >> 3) & 0xf;
369
370 /* compound payloads are not supported */
371 if (rtppayload[1] >> 7)
372 return -ENOTSUP;
373
Jean-Francois Dionned02c8af2017-01-10 10:23:56 +0100374 if (payload_len < amr_len_by_ft[type])
Max92db1502016-05-25 18:13:51 +0200375 return -ENOTSUP;
376
Max92db1502016-05-25 18:13:51 +0200377 if (ft)
378 *ft = type;
379
380 if (cmr)
381 *cmr = rtppayload[0] >> 4;
382
383 if (bfi)
384 *bfi = (rtppayload[1] >> 2) & 1;
385
386 /* Table 6 in 3GPP TS 26.101 */
387 if (cmi)
388 *cmi = (type == AMR_SID) ? ((rtppayload[6] >> 1) & 7) : -1;
389
390 if (sti)
391 *sti = (type == AMR_SID) ? (rtppayload[6] & 0x10) : -1;
392
393 return 2 + amr_len_by_ft[type];
394}
395
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200396/*! Encode various AMR parameters from RTP payload (RFC 4867)
Max92db1502016-05-25 18:13:51 +0200397 * \param[out] payload Payload for RTP packet, contains speech data (if any)
398 * except for have 2 first bytes where header will be built
399 * \param[in] cmr AMR codec Mode Request
400 * \param[in] ft AMR Frame Type
401 * \param[in] bfi AMR Bad Frame Indicator
402 * \returns length of AMR data (header + ToC + speech data) or negative value
403 * on error
404 *
405 * Note: only octet-aligned mode is supported so the header occupies 2 full
406 * bytes. Optional interleaving header is not supported.
407 */
408int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft,
409 enum osmo_amr_quality bfi)
410{
411 if (cmr > 15)
412 return -EINVAL;
413
414 if (ft > 15)
415 return -ENOTSUP;
416
417 /* RFC 4867 § 4.3.1 payload header */
418 payload[0] = cmr << 4;
419
420 /* RFC 4867 § 4.4.2 ToC - compound payloads are not supported: F = 0 */
421 payload[1] = (((uint8_t)ft) << 3) | (((uint8_t)bfi) << 2);
422
423 /* speech data */
424 return 2 + amr_len_by_ft[ft];
425}