blob: 69b8ed1e6b949daf66c8954829c97006288659ed [file] [log] [blame]
Neels Hofmeyrb2ce7482022-01-13 18:17:56 +01001/* Routines for translation between codec representations: SDP, CC/BSSMAP variants, MGCP, MNCC */
2/*
3 * (C) 2019-2023 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: AGPL-3.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23#include <string.h>
24
25#include <osmocom/gsm/mncc.h>
26
27#include <osmocom/msc/sdp_msg.h>
28#include <osmocom/msc/codec_mapping.h>
29#include <osmocom/msc/mncc.h>
30
31const struct codec_mapping codec_map[] = {
32 /* FIXME: sdp.fmtp handling is not done properly yet, proper mode-set and octet-align handling will follow in
33 * separate patches. */
34 {
35 .sdp = {
36 .payload_type = 0,
37 .subtype_name = "PCMU",
38 .rate = 8000,
39 },
40 .mgcp = CODEC_PCMU_8000_1,
41 },
42 {
43 .sdp = {
44 .payload_type = 3,
45 .subtype_name = "GSM",
46 .rate = 8000,
47 },
48 .mgcp = CODEC_GSM_8000_1,
49 .speech_ver_count = 1,
50 .speech_ver = { GSM48_BCAP_SV_FR },
51 .mncc_payload_msg_type = GSM_TCHF_FRAME,
52 .has_gsm0808_speech_codec = true,
53 .gsm0808_speech_codec = {
54 .fi = true,
55 .type = GSM0808_SCT_FR1,
56 },
57 .perm_speech = GSM0808_PERM_FR1,
58 .frhr = CODEC_FRHR_FR,
59 },
60 {
61 .sdp = {
62 .payload_type = 8,
63 .subtype_name = "PCMA",
64 .rate = 8000,
65 },
66 .mgcp = CODEC_PCMA_8000_1,
67 },
68 {
69 .sdp = {
70 .payload_type = 18,
71 .subtype_name = "G729",
72 .rate = 8000,
73 },
74 .mgcp = CODEC_G729_8000_1,
75 },
76 {
77 .sdp = {
78 .payload_type = 110,
79 .subtype_name = "GSM-EFR",
80 .rate = 8000,
81 },
82 .mgcp = CODEC_GSMEFR_8000_1,
83 .speech_ver_count = 1,
84 .speech_ver = { GSM48_BCAP_SV_EFR },
85 .mncc_payload_msg_type = GSM_TCHF_FRAME_EFR,
86 .has_gsm0808_speech_codec = true,
87 .gsm0808_speech_codec = {
88 .fi = true,
89 .type = GSM0808_SCT_FR2,
90 },
91 .perm_speech = GSM0808_PERM_FR2,
92 .frhr = CODEC_FRHR_FR,
93 },
94 {
95 .sdp = {
96 .payload_type = 111,
97 .subtype_name = "GSM-HR-08",
98 .rate = 8000,
99 },
100 .mgcp = CODEC_GSMHR_8000_1,
101 .speech_ver_count = 1,
102 .speech_ver = { GSM48_BCAP_SV_HR },
103 .mncc_payload_msg_type = GSM_TCHH_FRAME,
104 .has_gsm0808_speech_codec = true,
105 .gsm0808_speech_codec = {
106 .fi = true,
107 .type = GSM0808_SCT_HR1,
108 .cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR,
109 },
110 .perm_speech = GSM0808_PERM_HR1,
111 .frhr = CODEC_FRHR_HR,
112 },
113 {
114 .sdp = {
115 .payload_type = 112,
116 .subtype_name = "AMR",
117 .rate = 8000,
118 /* AMR is always octet-aligned in 2G and 3G RAN, so this fmtp is signalled to remote call legs.
119 * So far, fmtp is ignored in incoming SIP SDP, so an incoming SDP without 'octet-align=1' will
120 * match with this entry; we will still reply with 'octet-align=1', which often works out. */
121 .fmtp = "octet-align=1",
122 },
123 .mgcp = CODEC_AMR_8000_1,
124 .speech_ver_count = 1,
125 .speech_ver = { GSM48_BCAP_SV_AMR_F },
126 .mncc_payload_msg_type = GSM_TCH_FRAME_AMR,
127 .has_gsm0808_speech_codec = true,
128 .gsm0808_speech_codec = {
129 .fi = true,
130 .type = GSM0808_SCT_FR3,
131 .cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR,
132 },
133 .perm_speech = GSM0808_PERM_FR3,
134 .frhr = CODEC_FRHR_FR,
135 },
136 {
137 /* Another entry like the above, to map HR3 to AMR, too. */
138 .sdp = {
139 .payload_type = 112,
140 .subtype_name = "AMR",
141 .rate = 8000,
142 .fmtp = "octet-align=1",
143 },
144 .mgcp = CODEC_AMR_8000_1,
145 .speech_ver_count = 2,
146 .speech_ver = { GSM48_BCAP_SV_AMR_H, GSM48_BCAP_SV_AMR_OH },
147 .mncc_payload_msg_type = GSM_TCH_FRAME_AMR,
148 .has_gsm0808_speech_codec = true,
149 .gsm0808_speech_codec = {
150 .fi = true,
151 .type = GSM0808_SCT_HR3,
152 .cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR,
153 },
154 .perm_speech = GSM0808_PERM_HR3,
155 .frhr = CODEC_FRHR_HR,
156 },
157 {
158 .sdp = {
159 .payload_type = 113,
160 .subtype_name = "AMR-WB",
161 .rate = 16000,
162 .fmtp = "octet-align=1",
163 },
164 .mgcp = CODEC_AMRWB_16000_1,
165 .speech_ver_count = 2,
166 .speech_ver = { GSM48_BCAP_SV_AMR_OFW, GSM48_BCAP_SV_AMR_FW },
167 .mncc_payload_msg_type = GSM_TCH_FRAME_AMR,
168 .has_gsm0808_speech_codec = true,
169 .gsm0808_speech_codec = {
170 .fi = true,
171 .type = GSM0808_SCT_FR5,
172 .cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB,
173 },
174 .perm_speech = GSM0808_PERM_FR5,
175 .frhr = CODEC_FRHR_FR,
176 },
177 {
178 /* Another entry like the above, to map HR4 to AMR-WB, too. */
179 .sdp = {
180 .payload_type = 113,
181 .subtype_name = "AMR-WB",
182 .rate = 16000,
183 .fmtp = "octet-align=1",
184 },
185 .mgcp = CODEC_AMRWB_16000_1,
186 .speech_ver_count = 1,
187 .speech_ver = { GSM48_BCAP_SV_AMR_OHW },
188 .mncc_payload_msg_type = GSM_TCH_FRAME_AMR,
189 .has_gsm0808_speech_codec = true,
190 .gsm0808_speech_codec = {
191 .fi = true,
192 .type = GSM0808_SCT_HR4,
193 .cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB,
194 },
195 .perm_speech = GSM0808_PERM_HR4,
196 .frhr = CODEC_FRHR_HR,
197 },
198 {
199 .sdp = {
200 .payload_type = 96,
201 .subtype_name = "VND.3GPP.IUFP",
202 .rate = 16000,
203 },
204 .mgcp = CODEC_IUFP,
205 },
206};
207
208#define foreach_codec_mapping(CODEC_MAPPING) \
209 for ((CODEC_MAPPING) = codec_map; (CODEC_MAPPING) < codec_map + ARRAY_SIZE(codec_map); (CODEC_MAPPING)++)
210
211const struct gsm_mncc_bearer_cap bearer_cap_empty = {
212 .speech_ver = { -1 },
213 };
214
215const struct codec_mapping *codec_mapping_by_speech_ver(enum gsm48_bcap_speech_ver speech_ver)
216{
217 const struct codec_mapping *m;
218 foreach_codec_mapping(m) {
219 int i;
220 for (i = 0; i < m->speech_ver_count; i++)
221 if (m->speech_ver[i] == speech_ver)
222 return m;
223 }
224 return NULL;
225}
226
227const struct codec_mapping *codec_mapping_by_gsm0808_speech_codec_type(enum gsm0808_speech_codec_type sct)
228{
229 const struct codec_mapping *m;
230 foreach_codec_mapping(m) {
231 if (!m->has_gsm0808_speech_codec)
232 continue;
233 if (m->gsm0808_speech_codec.type == sct)
234 return m;
235 }
236 return NULL;
237}
238
239const struct codec_mapping *codec_mapping_by_gsm0808_speech_codec(const struct gsm0808_speech_codec *sc)
240{
241 const struct codec_mapping *m;
242 foreach_codec_mapping(m) {
243 if (!m->has_gsm0808_speech_codec)
244 continue;
245 if (m->gsm0808_speech_codec.type != sc->type)
246 continue;
247 /* Return only those where sc->cfg is a subset of m->gsm0808_speech_codec.cfg. */
248 if ((m->gsm0808_speech_codec.cfg & sc->cfg) != sc->cfg)
249 continue;
250 return m;
251 }
252 return NULL;
253}
254
255const struct codec_mapping *codec_mapping_by_perm_speech(enum gsm0808_permitted_speech perm_speech)
256{
257 const struct codec_mapping *m;
258 foreach_codec_mapping(m) {
259 if (m->perm_speech == perm_speech)
260 return m;
261 }
262 return NULL;
263}
264
265const struct codec_mapping *codec_mapping_by_subtype_name(const char *subtype_name)
266{
267 const struct codec_mapping *m;
268 foreach_codec_mapping(m) {
269 if (!strcmp(m->sdp.subtype_name, subtype_name))
270 return m;
271 }
272 return NULL;
273}
274
275const struct codec_mapping *codec_mapping_by_mgcp_codec(enum mgcp_codecs mgcp)
276{
277 const struct codec_mapping *m;
278 foreach_codec_mapping(m) {
279 if (m->mgcp == mgcp)
280 return m;
281 }
282 return NULL;
283}
284
285/* Append given Speech Version to the end of the Bearer Capabilities Speech Version array. Return 1 if added, zero
286 * otherwise (as in, return the number of items added). */
287int bearer_cap_add_speech_ver(struct gsm_mncc_bearer_cap *bearer_cap, enum gsm48_bcap_speech_ver speech_ver)
288{
289 int i;
290 for (i = 0; i < ARRAY_SIZE(bearer_cap->speech_ver) - 1; i++) {
291 if (bearer_cap->speech_ver[i] == speech_ver)
292 return 0;
293 if (bearer_cap->speech_ver[i] == -1) {
294 bearer_cap->speech_ver[i] = speech_ver;
295 bearer_cap->speech_ver[i+1] = -1;
296 return 1;
297 }
298 }
299 return 0;
300}
301
302/* From the current speech_ver list present in the bearer_cap, set the bearer_cap.radio.
303 * If a HR speech_ver is present, set to GSM48_BCAP_RRQ_DUAL_FR, otherwise set to GSM48_BCAP_RRQ_FR_ONLY. */
304int bearer_cap_set_radio(struct gsm_mncc_bearer_cap *bearer_cap)
305{
306 bool hr_present = false;
307 int i;
308 for (i = 0; i < ARRAY_SIZE(bearer_cap->speech_ver) - 1; i++) {
309 const struct codec_mapping *m;
310
311 if (bearer_cap->speech_ver[i] == -1)
312 break;
313
314 m = codec_mapping_by_speech_ver(bearer_cap->speech_ver[i]);
315
316 if (!m)
317 continue;
318
319 if (m->frhr == CODEC_FRHR_HR)
320 hr_present = true;
321 }
322
323 if (hr_present)
324 bearer_cap->radio = GSM48_BCAP_RRQ_DUAL_FR;
325 else
326 bearer_cap->radio = GSM48_BCAP_RRQ_FR_ONLY;
327
328 return 0;
329}
330
331/* Try to convert the SDP audio codec name to Speech Versions to append to Bearer Capabilities.
332 * Return the number of Speech Version entries added (some may add more than one, others may be unknown/unapplicable and
333 * return 0). */
334int sdp_audio_codec_add_to_bearer_cap(struct gsm_mncc_bearer_cap *bearer_cap, const struct sdp_audio_codec *codec)
335{
336 const struct codec_mapping *m;
337 int added = 0;
338 foreach_codec_mapping(m) {
339 int i;
340 if (strcmp(m->sdp.subtype_name, codec->subtype_name))
341 continue;
342 /* TODO also match rate and fmtp? */
343 for (i = 0; i < m->speech_ver_count; i++)
344 added += bearer_cap_add_speech_ver(bearer_cap, m->speech_ver[i]);
345 }
346 return added;
347}
348
349/* Append all audio codecs found in given sdp_msg to Bearer Capability, by traversing all codec entries with
350 * sdp_audio_codec_add_to_bearer_cap(). Return the number of Speech Version entries added.
351 * Note that Speech Version entries are only appended, no previous entries are removed.
352 * Note that only the Speech Version entries are modified; to make a valid Bearer Capabiliy, at least bearer_cap->radio
353 * must also be set (before or after this function); see also bearer_cap_set_radio(). */
354int sdp_audio_codecs_to_bearer_cap(struct gsm_mncc_bearer_cap *bearer_cap, const struct sdp_audio_codecs *ac)
355{
356 const struct sdp_audio_codec *codec;
357 int added = 0;
358
359 foreach_sdp_audio_codec(codec, ac) {
360 added += sdp_audio_codec_add_to_bearer_cap(bearer_cap, codec);
361 }
362
363 return added;
364}
365
366/* Convert Speech Version to SDP audio codec and append to SDP message struct. */
367struct sdp_audio_codec *sdp_audio_codecs_add_speech_ver(struct sdp_audio_codecs *ac,
368 enum gsm48_bcap_speech_ver speech_ver)
369{
370 const struct codec_mapping *m;
371 struct sdp_audio_codec *ret = NULL;
372 foreach_codec_mapping(m) {
373 int i;
374 for (i = 0; i < m->speech_ver_count; i++) {
375 if (m->speech_ver[i] == speech_ver) {
376 ret = sdp_audio_codecs_add_copy(ac, &m->sdp);
377 break;
378 }
379 }
380 }
381 return ret;
382}
383
384struct sdp_audio_codec *sdp_audio_codecs_add_mgcp_codec(struct sdp_audio_codecs *ac, enum mgcp_codecs mgcp_codec)
385{
386 const struct codec_mapping *m = codec_mapping_by_mgcp_codec(mgcp_codec);
387 if (!m)
388 return NULL;
389 return sdp_audio_codecs_add_copy(ac, &m->sdp);
390}
391
392void sdp_audio_codecs_from_bearer_cap(struct sdp_audio_codecs *ac, const struct gsm_mncc_bearer_cap *bc)
393{
394 unsigned int i;
395
396 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
397 if (bc->speech_ver[i] == -1)
398 break;
399 sdp_audio_codecs_add_speech_ver(ac, bc->speech_ver[i]);
400 }
401}
402
403void sdp_audio_codecs_to_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct sdp_audio_codecs *ac)
404{
405 const struct sdp_audio_codec *codec;
406
407 *scl = (struct gsm0808_speech_codec_list){};
408
409 foreach_sdp_audio_codec(codec, ac) {
410 const struct codec_mapping *m = codec_mapping_by_subtype_name(codec->subtype_name);
411 if (!m)
412 continue;
413 if (!m->has_gsm0808_speech_codec)
414 continue;
415 if (scl->len >= ARRAY_SIZE(scl->codec))
416 break;
417 scl->codec[scl->len] = m->gsm0808_speech_codec;
418 /* FIXME: apply AMR configuration according to codec->fmtp */
419 scl->len++;
420 }
421}
422
423void sdp_audio_codecs_from_speech_codec_list(struct sdp_audio_codecs *ac, const struct gsm0808_speech_codec_list *cl)
424{
425 int i;
426 for (i = 0; i < cl->len; i++) {
427 const struct gsm0808_speech_codec *sc = &cl->codec[i];
428 const struct codec_mapping *m = codec_mapping_by_gsm0808_speech_codec(sc);
429 if (!m)
430 continue;
431 sdp_audio_codecs_add_copy(ac, &m->sdp);
432 /* FIXME: for AMR, apply sc->cfg to the added codec's fmtp */
433 }
434}
435
436int sdp_audio_codecs_to_gsm0808_channel_type(struct gsm0808_channel_type *ct, const struct sdp_audio_codecs *ac)
437{
438 const struct sdp_audio_codec *codec;
439 bool fr_present = false;
440 int first_fr_idx = -1;
441 bool hr_present = false;
442 int first_hr_idx = -1;
443 int idx = -1;
444
445 *ct = (struct gsm0808_channel_type){
446 .ch_indctr = GSM0808_CHAN_SPEECH,
447 };
448
449 foreach_sdp_audio_codec(codec, ac) {
450 const struct codec_mapping *m;
451 int i;
452 bool dup;
453 idx++;
454 foreach_codec_mapping(m) {
455 if (strcmp(m->sdp.subtype_name, codec->subtype_name))
456 continue;
457
458 switch (m->perm_speech) {
459 default:
460 continue;
461
462 case GSM0808_PERM_FR1:
463 case GSM0808_PERM_FR2:
464 case GSM0808_PERM_FR3:
465 case GSM0808_PERM_FR4:
466 case GSM0808_PERM_FR5:
467 fr_present = true;
468 if (first_fr_idx < 0)
469 first_fr_idx = idx;
470 break;
471
472 case GSM0808_PERM_HR1:
473 case GSM0808_PERM_HR2:
474 case GSM0808_PERM_HR3:
475 case GSM0808_PERM_HR4:
476 case GSM0808_PERM_HR6:
477 hr_present = true;
478 if (first_hr_idx < 0)
479 first_hr_idx = idx;
480 break;
481 }
482
483 /* Avoid duplicates */
484 dup = false;
485 for (i = 0; i < ct->perm_spch_len; i++) {
486 if (ct->perm_spch[i] == m->perm_speech) {
487 dup = true;
488 break;
489 }
490 }
491 if (dup)
492 continue;
493
494 ct->perm_spch[ct->perm_spch_len] = m->perm_speech;
495 ct->perm_spch_len++;
496 }
497 }
498
499 if (fr_present && hr_present) {
500 if (first_fr_idx <= first_hr_idx)
501 ct->ch_rate_type = GSM0808_SPEECH_FULL_PREF;
502 else
503 ct->ch_rate_type = GSM0808_SPEECH_HALF_PREF;
504 } else if (fr_present && !hr_present)
505 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
506 else if (!fr_present && hr_present)
507 ct->ch_rate_type = GSM0808_SPEECH_HALF_LM;
508 else
509 return -EINVAL;
510 return 0;
511}
512
513enum mgcp_codecs sdp_audio_codec_to_mgcp_codec(const struct sdp_audio_codec *codec)
514{
515 const struct codec_mapping *m;
516 foreach_codec_mapping(m) {
517 if (!sdp_audio_codec_cmp(&m->sdp, codec, false, false))
518 return m->mgcp;
519 }
520 return NO_MGCP_CODEC;
521}