blob: ef1d3b9bb910edab5bc7606d314bb4763219793f [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm620.c
2 * GSM 06.20 - GSM HR codec. */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +02003/*
4 * (C) 2010 Sylvain Munaut <tnt@246tNt.com>
5 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020020 */
21
22#include <stdint.h>
Maxec8f1922016-05-31 14:50:21 +020023#include <stdbool.h>
Mychaela N. Falconia18e5af52023-05-26 21:15:06 +000024#include <string.h>
Maxec8f1922016-05-31 14:50:21 +020025
26#include <osmocom/core/bitvec.h>
27#include <osmocom/core/utils.h>
Harald Weltedee71722017-05-31 02:48:48 +020028#include <osmocom/codec/codec.h>
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020029
30/* GSM HR unvoiced (mode=0) frames - subjective importance bit ordering */
31 /* This array encode mapping between GSM 05.03 Table 3a (bits
32 * ordering before channel coding on TCH) and GSM 06.20 Table B.1
33 * (bit ordering on A-bis */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -070034const uint16_t gsm620_unvoiced_bitorder[112] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020035 3, /* R0:1 */
36 25, /* LPC 3:7 */
37 52, /* GSP 0-1:2 */
38 71, /* GSP 0-2:2 */
39 90, /* GSP 0-3:2 */
40 109, /* GSP 0-4:2 */
41 15, /* LPC 1:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020042 19, /* LPC 2:5 */
Sylvain Munauted8170a2013-03-01 16:35:46 +010043 20, /* LPC 2:4 */
44 21, /* LPC 2:3 */
45 22, /* LPC 2:2 */
46 23, /* LPC 2:1 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020047 26, /* LPC 3:6 */
Sylvain Munauted8170a2013-03-01 16:35:46 +010048 27, /* LPC 3:5 */
49 28, /* LPC 3:4 */
50 29, /* LPC 3:3 */
51 30, /* LPC 3:2 */
52 31, /* LPC 3:1 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020053 61, /* Code 1-2:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020054 62, /* Code 2-2:6 */
Sylvain Munauted8170a2013-03-01 16:35:46 +010055 63, /* Code 2-2:5 */
56 64, /* Code 2-2:4 */
57 65, /* Code 2-2:3 */
58 66, /* Code 2-2:2 */
59 67, /* Code 2-2:1 */
60 68, /* Code 2-2:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020061 74, /* Code 1-3:6 */
Sylvain Munauted8170a2013-03-01 16:35:46 +010062 75, /* Code 1-3:5 */
63 76, /* Code 1-3:4 */
64 77, /* Code 1-3:3 */
65 78, /* Code 1-3:2 */
66 79, /* Code 1-3:1 */
67 80, /* Code 1-3:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020068 81, /* Code 2-3:6 */
Sylvain Munauted8170a2013-03-01 16:35:46 +010069 82, /* Code 2-3:5 */
70 83, /* Code 2-3:4 */
71 84, /* Code 2-3:3 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020072 32, /* LPC 3:0 */
73 4, /* R0:0 */
74 33, /* INT-LPC:0 */
75 60, /* Code 1-2:1 */
76 59, /* Code 1-2:2 */
77 58, /* Code 1-2:3 */
78 57, /* Code 1-2:4 */
79 56, /* Code 1-2:5 */
80 55, /* Code 1-2:6 */
81 49, /* Code 2-1:0 */
82 48, /* Code 2-1:1 */
83 47, /* Code 2-1:2 */
84 46, /* Code 2-1:3 */
85 45, /* Code 2-1:4 */
86 44, /* Code 2-1:5 */
87 43, /* Code 2-1:6 */
88 42, /* Code 1-1:0 */
89 41, /* Code 1-1:1 */
90 40, /* Code 1-1:2 */
91 39, /* Code 1-1:3 */
92 38, /* Code 1-1:4 */
93 37, /* Code 1-1:5 */
94 36, /* Code 1-1:6 */
95 111, /* GSP 0-4:0 */
96 92, /* GSP 0-3:0 */
97 73, /* GSP 0-2:0 */
98 54, /* GSP 0-1:0 */
99 24, /* LPC 2:0 */
100 110, /* GSP 0-4:1 */
101 91, /* GSP 0-3:1 */
102 72, /* GSP 0-2:1 */
103 53, /* GSP 0-1:1 */
104 14, /* LPC 1:1 */
105 13, /* LPC 1:2 */
106 12, /* LPC 1:3 */
107 11, /* LPC 1:4 */
108 10, /* LPC 1:5 */
109 108, /* GSP 0-4:3 */
110 89, /* GSP 0-3:3 */
111 70, /* GSP 0-2:3 */
112 51, /* GSP 0-1:3 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200113 16, /* LPC 2:8 */
Sylvain Munauted8170a2013-03-01 16:35:46 +0100114 17, /* LPC 2:7 */
115 18, /* LPC 2:6 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200116 107, /* GSP 0-4:4 */
117 88, /* GSP 0-3:4 */
118 69, /* GSP 0-2:4 */
119 50, /* GSP 0-1:4 */
120 9, /* LPC 1:6 */
121 8, /* LPC 1:7 */
122 7, /* LPC 1:8 */
123 6, /* LPC 1:9 */
124 2, /* R0:2 */
125 5, /* LPC 1:10 */
126 1, /* R0:3 */
127 0, /* R0:4 */
128 35, /* Mode:0 */
129 34, /* Mode:1 */
130 106, /* Code 2-4:0 */
131 105, /* Code 2-4:1 */
132 104, /* Code 2-4:2 */
133 103, /* Code 2-4:3 */
134 102, /* Code 2-4:4 */
135 101, /* Code 2-4:5 */
136 100, /* Code 2-4:6 */
137 99, /* Code 1-4:0 */
138 98, /* Code 1-4:1 */
139 97, /* Code 1-4:2 */
140 96, /* Code 1-4:3 */
141 95, /* Code 1-4:4 */
142 94, /* Code 1-4:5 */
143 93, /* Code 1-4:6 */
144 87, /* Code 2-3:0 */
145 86, /* Code 2-3:1 */
146 85, /* Code 2-3:2 */
147};
148
149/* GSM HR voiced (mode=1,2,3) frames - subjective importance bit ordering */
150 /* This array encode mapping between GSM 05.03 Table 3b (bits
151 * ordering before channel coding on TCH) and GSM 06.20 Table B.2
152 * (bit ordering on A-bis */
Diego Elio Pettenò23431c72012-06-29 13:01:27 -0700153const uint16_t gsm620_voiced_bitorder[112] = {
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200154 13, /* LPC 1:2 */
155 14, /* LPC 1:1 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200156 18, /* LPC 2:6 */
Sylvain Munauted8170a2013-03-01 16:35:46 +0100157 19, /* LPC 2:5 */
158 20, /* LPC 2:4 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200159 53, /* GSP 0-1:4 */
160 71, /* GSP 0-2:4 */
161 89, /* GSP 0-3:4 */
162 107, /* GSP 0-4:4 */
163 54, /* GSP 0-1:3 */
164 72, /* GSP 0-2:3 */
165 90, /* GSP 0-3:3 */
166 108, /* GSP 0-4:3 */
167 55, /* GSP 0-1:2 */
168 73, /* GSP 0-2:2 */
169 91, /* GSP 0-3:2 */
170 109, /* GSP 0-4:2 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200171 44, /* Code 1:8 */
Sylvain Munauted8170a2013-03-01 16:35:46 +0100172 45, /* Code 1:7 */
173 46, /* Code 1:6 */
174 47, /* Code 1:5 */
175 48, /* Code 1:4 */
176 49, /* Code 1:3 */
177 50, /* Code 1:2 */
178 51, /* Code 1:1 */
179 52, /* Code 1:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200180 62, /* Code 2:8 */
Sylvain Munauted8170a2013-03-01 16:35:46 +0100181 63, /* Code 2:7 */
182 64, /* Code 2:6 */
183 65, /* Code 2:5 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200184 68, /* Code 2:2 */
Sylvain Munauted8170a2013-03-01 16:35:46 +0100185 69, /* Code 2:1 */
186 70, /* Code 2:0 */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +0200187 80, /* Code 3:8 */
188 66, /* Code 2:4 */
189 67, /* Code 2:3 */
190 56, /* GSP 0-1:1 */
191 74, /* GSP 0-2:1 */
192 92, /* GSP 0-3:1 */
193 110, /* GSP 0-4:1 */
194 57, /* GSP 0-1:0 */
195 75, /* GSP 0-2:0 */
196 93, /* GSP 0-3:0 */
197 111, /* GSP 0-4:0 */
198 33, /* INT-LPC:0 */
199 24, /* LPC 2:0 */
200 32, /* LPC 3:0 */
201 97, /* LAG 4:0 */
202 31, /* LPC 3:1 */
203 23, /* LPC 2:1 */
204 96, /* LAG 4:1 */
205 79, /* LAG 3:0 */
206 61, /* LAG 2:0 */
207 43, /* LAG 1:0 */
208 95, /* LAG 4:2 */
209 78, /* LAG 3:1 */
210 60, /* LAG 2:1 */
211 42, /* LAG 1:1 */
212 30, /* LPC 3:2 */
213 29, /* LPC 3:3 */
214 28, /* LPC 3:4 */
215 22, /* LPC 2:2 */
216 27, /* LPC 3:5 */
217 26, /* LPC 3:6 */
218 21, /* LPC 2:3 */
219 4, /* R0:0 */
220 25, /* LPC 3:7 */
221 15, /* LPC 1:0 */
222 94, /* LAG 4:3 */
223 77, /* LAG 3:2 */
224 59, /* LAG 2:2 */
225 41, /* LAG 1:2 */
226 3, /* R0:1 */
227 76, /* LAG 3:3 */
228 58, /* LAG 2:3 */
229 40, /* LAG 1:3 */
230 39, /* LAG 1:4 */
231 17, /* LPC 2:7 */
232 16, /* LPC 2:8 */
233 12, /* LPC 1:3 */
234 11, /* LPC 1:4 */
235 10, /* LPC 1:5 */
236 9, /* LPC 1:6 */
237 2, /* R0:2 */
238 38, /* LAG 1:5 */
239 37, /* LAG 1:6 */
240 36, /* LAG 1:7 */
241 8, /* LPC 1:7 */
242 7, /* LPC 1:8 */
243 6, /* LPC 1:9 */
244 5, /* LPC 1:10 */
245 1, /* R0:3 */
246 0, /* R0:4 */
247 35, /* Mode:0 */
248 34, /* Mode:1 */
249 106, /* Code 4:0 */
250 105, /* Code 4:1 */
251 104, /* Code 4:2 */
252 103, /* Code 4:3 */
253 102, /* Code 4:4 */
254 101, /* Code 4:5 */
255 100, /* Code 4:6 */
256 99, /* Code 4:7 */
257 98, /* Code 4:8 */
258 88, /* Code 3:0 */
259 87, /* Code 3:1 */
260 86, /* Code 3:2 */
261 85, /* Code 3:3 */
262 84, /* Code 3:4 */
263 83, /* Code 3:5 */
264 82, /* Code 3:6 */
265 81, /* Code 3:7 */
266};
Maxec8f1922016-05-31 14:50:21 +0200267
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200268/*! Check whether RTP frame contains HR SID code word according to
Maxec8f1922016-05-31 14:50:21 +0200269 * TS 101 318 §5.2.2
270 * \param[in] rtp_payload Buffer with RTP payload
271 * \param[in] payload_len Length of payload
272 * \returns true if code word is found, false otherwise
273 */
Harald Welte6789ba32017-05-31 02:47:43 +0200274bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len)
Maxec8f1922016-05-31 14:50:21 +0200275{
Vadim Yanitskiy7ec19de2022-03-10 01:43:03 +0300276 struct bitvec bv = {
277 .data = (uint8_t *)rtp_payload,
278 .data_len = payload_len,
279 };
Maxec8f1922016-05-31 14:50:21 +0200280
Vadim Yanitskiy7ec19de2022-03-10 01:43:03 +0300281 /* A SID frame is identified by a SID codeword consisting of 79 bits which are all 1,
282 * so we basically check if all bits in range r34..r112 (inclusive) are 1. */
283 for (bv.cur_bit = 33; bv.cur_bit < bv.data_len * 8; bv.cur_bit++)
284 if (bitvec_get_bit_pos(&bv, bv.cur_bit) != ONE)
Maxec8f1922016-05-31 14:50:21 +0200285 return false;
286
287 return true;
288}
Mychaela N. Falconia18e5af52023-05-26 21:15:06 +0000289
290/*! Reset the SID field of a potentially corrupted, but still valid GSM-HR
291 * SID frame in TS 101 318 format to its pristine state (full SID codeword).
292 * \param[in] rtp_payload Buffer with RTP payload - must be writable!
293 *
294 * Per GSM 06.22 section 5.3, a freshly minted SID frame consists of 33 bits
295 * of comfort noise parameters and 79 bits of SID codeword (all 1s). Network
296 * elements that receive SID frames from call leg A uplink and need to
297 * retransmit them on leg B downlink should "rejuvenate" received SID frames
298 * prior to retransmission by resetting the SID field to its pristine state
299 * of all 1s; this function does the job.
300 *
301 * Important note: because of HR-specific quirks (lack of exact bit counting
302 * rules in GSM 06.41 spec compared to 06.31 & 06.81, plus the fact that such
303 * bit counting can only be done efficiently in the GSM 05.03 channel decoder
304 * prior to bit reordering based on voiced or unvoiced mode), a generic
305 * (usable from any network element) SID classification function similar to
306 * osmo_{fr,efr}_sid_classify() unfortunately cannot exist for HR. Therefore,
307 * the triggering condition for invoking this SID rejuvenation/reset function
308 * can only be an out-of-band SID indication, as in GSM 08.61 TRAU frames
309 * or RFC 5993 ToC octet.
310 */
311void osmo_hr_sid_reset(uint8_t *rtp_payload)
312{
313 /* set all 79 SID codeword bits to 1 */
314 rtp_payload[4] |= 0x7F;
315 memset(rtp_payload + 5, 0xFF, 9);
316}