blob: 7f8243229389e78e38fae5317fed4003b25a9139 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file codec.h */
2
Sylvain Munaut12ba7782014-06-16 10:13:40 +02003#pragma once
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +02004
5#include <stdint.h>
Maxec8f1922016-05-31 14:50:21 +02006#include <stdbool.h>
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +02007
Max92db1502016-05-25 18:13:51 +02008#include <osmocom/core/utils.h>
Harald Welte5785a4a2020-05-13 23:10:31 +02009#include <osmocom/core/bits.h>
Max92db1502016-05-25 18:13:51 +020010
Vadim Yanitskiye0941572017-12-16 01:39:48 +070011/* TS 101318 Chapter 5.1: 260 bits + 4bit sig */
12#define GSM_FR_BYTES 33
13/* TS 101318 Chapter 5.2: 112 bits, no sig */
14#define GSM_HR_BYTES 14
15/* TS 101318 Chapter 5.3: 244 bits + 4bit sig */
16#define GSM_EFR_BYTES 31
17
Philipp Maier4cf72df2023-05-03 15:01:23 +020018/* Number of bytes of an GSM_HR RTP payload */
19#define GSM_HR_BYTES_RTP_RFC5993 (GSM_HR_BYTES + 1)
20#define GSM_HR_BYTES_RTP_TS101318 (GSM_HR_BYTES)
21
Diego Elio Pettenò23431c72012-06-29 13:01:27 -070022extern const uint16_t gsm610_bitorder[]; /* FR */
23extern const uint16_t gsm620_unvoiced_bitorder[]; /* HR unvoiced */
24extern const uint16_t gsm620_voiced_bitorder[]; /* HR voiced */
25extern const uint16_t gsm660_bitorder[]; /* EFR */
Sylvain Munaut1a4ea5b2010-10-08 15:09:16 +020026
Diego Elio Pettenò23431c72012-06-29 13:01:27 -070027extern const uint16_t gsm690_12_2_bitorder[]; /* AMR 12.2 kbits */
28extern const uint16_t gsm690_10_2_bitorder[]; /* AMR 10.2 kbits */
29extern const uint16_t gsm690_7_95_bitorder[]; /* AMR 7.95 kbits */
30extern const uint16_t gsm690_7_4_bitorder[]; /* AMR 7.4 kbits */
31extern const uint16_t gsm690_6_7_bitorder[]; /* AMR 6.7 kbits */
32extern const uint16_t gsm690_5_9_bitorder[]; /* AMR 5.9 kbits */
33extern const uint16_t gsm690_5_15_bitorder[]; /* AMR 5.15 kbits */
34extern const uint16_t gsm690_4_75_bitorder[]; /* AMR 4.75 kbits */
Max92db1502016-05-25 18:13:51 +020035
36extern const struct value_string osmo_amr_type_names[];
37
38enum osmo_amr_type {
39 AMR_4_75 = 0,
40 AMR_5_15 = 1,
41 AMR_5_90 = 2,
42 AMR_6_70 = 3,
43 AMR_7_40 = 4,
44 AMR_7_95 = 5,
45 AMR_10_2 = 6,
46 AMR_12_2 = 7,
47 AMR_SID = 8,
48 AMR_GSM_EFR_SID = 9,
49 AMR_TDMA_EFR_SID = 10,
50 AMR_PDC_EFR_SID = 11,
51 AMR_NO_DATA = 15,
52};
53
Philipp Maier2bd4bc92021-08-23 15:42:51 +020054static inline const char *osmo_amr_type_name(enum osmo_amr_type type)
55{ return get_value_string(osmo_amr_type_names, type); }
56
Max92db1502016-05-25 18:13:51 +020057enum osmo_amr_quality {
58 AMR_BAD = 0,
59 AMR_GOOD = 1
60};
61
Harald Welte5785a4a2020-05-13 23:10:31 +020062extern const uint8_t gsm690_bitlength[AMR_NO_DATA+1];
63
64int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
65int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
66
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067/*! Check if given AMR Frame Type is a speech frame
Max74a8f082016-09-27 13:51:51 +020068 * \param[in] ft AMR Frame Type
69 * \returns true if AMR with given Frame Type contains voice, false otherwise
70 */
Max2bb65be2016-09-28 15:38:55 +020071static inline bool osmo_amr_is_speech(enum osmo_amr_type ft)
Max74a8f082016-09-27 13:51:51 +020072{
73 switch (ft) {
74 case AMR_4_75:
75 case AMR_5_15:
76 case AMR_5_90:
77 case AMR_6_70:
78 case AMR_7_40:
79 case AMR_7_95:
80 case AMR_10_2:
81 case AMR_12_2:
82 return true;
83 default:
84 return false;
85 }
86}
87
Mychaela N. Falconiaec650852023-04-16 01:34:35 +000088/* SID ternary classification per GSM 06.31 & 06.81 section 6.1.1 */
89enum osmo_gsm631_sid_class {
90 OSMO_GSM631_SID_CLASS_SPEECH = 0,
91 OSMO_GSM631_SID_CLASS_INVALID = 1,
92 OSMO_GSM631_SID_CLASS_VALID = 2,
93};
94
Harald Welte6789ba32017-05-31 02:47:43 +020095bool osmo_fr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
96bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
Mychaela N. Falconia7f918802023-03-24 00:46:12 +000097bool osmo_efr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
Mychaela N. Falconiaec650852023-04-16 01:34:35 +000098
99enum osmo_gsm631_sid_class osmo_fr_sid_classify(const uint8_t *rtp_payload);
100enum osmo_gsm631_sid_class osmo_efr_sid_classify(const uint8_t *rtp_payload);
Mychaela N. Falconia341c4ec2023-05-09 07:51:12 +0000101
102/*! Check if given FR codec frame is any kind of SID, valid or invalid
103 * \param[in] rtp_payload Buffer with RTP payload
104 * \returns true if the frame is an "accepted SID frame" in GSM 06.31
105 * definition, false otherwise.
106 */
107static inline bool osmo_fr_is_any_sid(const uint8_t *rtp_payload)
108{
109 enum osmo_gsm631_sid_class sidc;
110
111 sidc = osmo_fr_sid_classify(rtp_payload);
112 return sidc != OSMO_GSM631_SID_CLASS_SPEECH;
113}
114
115/*! Check if given EFR codec frame is any kind of SID, valid or invalid
116 * \param[in] rtp_payload Buffer with RTP payload
117 * \returns true if the frame is an "accepted SID frame" in GSM 06.81
118 * definition, false otherwise.
119 */
120static inline bool osmo_efr_is_any_sid(const uint8_t *rtp_payload)
121{
122 enum osmo_gsm631_sid_class sidc;
123
124 sidc = osmo_efr_sid_classify(rtp_payload);
125 return sidc != OSMO_GSM631_SID_CLASS_SPEECH;
126}
127
Mychaela N. Falconia295636b2023-04-16 17:19:50 +0000128bool osmo_fr_sid_preen(uint8_t *rtp_payload);
129bool osmo_efr_sid_preen(uint8_t *rtp_payload);
Mychaela N. Falconiaec650852023-04-16 01:34:35 +0000130
Max92db1502016-05-25 18:13:51 +0200131int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft,
132 enum osmo_amr_quality bfi);
133int osmo_amr_rtp_dec(const uint8_t *payload, int payload_len, uint8_t *cmr,
134 int8_t *cmi, enum osmo_amr_type *ft,
135 enum osmo_amr_quality *bfi, int8_t *sti);