Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file codec.h */ |
| 2 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 3 | #pragma once |
Sylvain Munaut | 1a4ea5b | 2010-10-08 15:09:16 +0200 | [diff] [blame] | 4 | |
| 5 | #include <stdint.h> |
Max | ec8f192 | 2016-05-31 14:50:21 +0200 | [diff] [blame] | 6 | #include <stdbool.h> |
Sylvain Munaut | 1a4ea5b | 2010-10-08 15:09:16 +0200 | [diff] [blame] | 7 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 8 | #include <osmocom/core/utils.h> |
Harald Welte | 5785a4a | 2020-05-13 23:10:31 +0200 | [diff] [blame] | 9 | #include <osmocom/core/bits.h> |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 10 | |
Vadim Yanitskiy | e094157 | 2017-12-16 01:39:48 +0700 | [diff] [blame] | 11 | /* 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 Maier | 4cf72df | 2023-05-03 15:01:23 +0200 | [diff] [blame] | 18 | /* 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ò | 23431c7 | 2012-06-29 13:01:27 -0700 | [diff] [blame] | 22 | extern const uint16_t gsm610_bitorder[]; /* FR */ |
| 23 | extern const uint16_t gsm620_unvoiced_bitorder[]; /* HR unvoiced */ |
| 24 | extern const uint16_t gsm620_voiced_bitorder[]; /* HR voiced */ |
| 25 | extern const uint16_t gsm660_bitorder[]; /* EFR */ |
Sylvain Munaut | 1a4ea5b | 2010-10-08 15:09:16 +0200 | [diff] [blame] | 26 | |
Diego Elio Pettenò | 23431c7 | 2012-06-29 13:01:27 -0700 | [diff] [blame] | 27 | extern const uint16_t gsm690_12_2_bitorder[]; /* AMR 12.2 kbits */ |
| 28 | extern const uint16_t gsm690_10_2_bitorder[]; /* AMR 10.2 kbits */ |
| 29 | extern const uint16_t gsm690_7_95_bitorder[]; /* AMR 7.95 kbits */ |
| 30 | extern const uint16_t gsm690_7_4_bitorder[]; /* AMR 7.4 kbits */ |
| 31 | extern const uint16_t gsm690_6_7_bitorder[]; /* AMR 6.7 kbits */ |
| 32 | extern const uint16_t gsm690_5_9_bitorder[]; /* AMR 5.9 kbits */ |
| 33 | extern const uint16_t gsm690_5_15_bitorder[]; /* AMR 5.15 kbits */ |
| 34 | extern const uint16_t gsm690_4_75_bitorder[]; /* AMR 4.75 kbits */ |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 35 | |
Mychaela N. Falconia | f86ec50 | 2023-05-10 18:18:42 +0000 | [diff] [blame] | 36 | extern const uint8_t osmo_gsm611_silence_frame[GSM_FR_BYTES]; |
| 37 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 38 | extern const struct value_string osmo_amr_type_names[]; |
| 39 | |
| 40 | enum osmo_amr_type { |
| 41 | AMR_4_75 = 0, |
| 42 | AMR_5_15 = 1, |
| 43 | AMR_5_90 = 2, |
| 44 | AMR_6_70 = 3, |
| 45 | AMR_7_40 = 4, |
| 46 | AMR_7_95 = 5, |
| 47 | AMR_10_2 = 6, |
| 48 | AMR_12_2 = 7, |
| 49 | AMR_SID = 8, |
| 50 | AMR_GSM_EFR_SID = 9, |
| 51 | AMR_TDMA_EFR_SID = 10, |
| 52 | AMR_PDC_EFR_SID = 11, |
| 53 | AMR_NO_DATA = 15, |
| 54 | }; |
| 55 | |
Philipp Maier | 2bd4bc9 | 2021-08-23 15:42:51 +0200 | [diff] [blame] | 56 | static inline const char *osmo_amr_type_name(enum osmo_amr_type type) |
| 57 | { return get_value_string(osmo_amr_type_names, type); } |
| 58 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 59 | enum osmo_amr_quality { |
| 60 | AMR_BAD = 0, |
| 61 | AMR_GOOD = 1 |
| 62 | }; |
| 63 | |
Harald Welte | 5785a4a | 2020-05-13 23:10:31 +0200 | [diff] [blame] | 64 | extern const uint8_t gsm690_bitlength[AMR_NO_DATA+1]; |
| 65 | |
| 66 | int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode); |
| 67 | int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode); |
| 68 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 69 | /*! Check if given AMR Frame Type is a speech frame |
Max | 74a8f08 | 2016-09-27 13:51:51 +0200 | [diff] [blame] | 70 | * \param[in] ft AMR Frame Type |
| 71 | * \returns true if AMR with given Frame Type contains voice, false otherwise |
| 72 | */ |
Max | 2bb65be | 2016-09-28 15:38:55 +0200 | [diff] [blame] | 73 | static inline bool osmo_amr_is_speech(enum osmo_amr_type ft) |
Max | 74a8f08 | 2016-09-27 13:51:51 +0200 | [diff] [blame] | 74 | { |
| 75 | switch (ft) { |
| 76 | case AMR_4_75: |
| 77 | case AMR_5_15: |
| 78 | case AMR_5_90: |
| 79 | case AMR_6_70: |
| 80 | case AMR_7_40: |
| 81 | case AMR_7_95: |
| 82 | case AMR_10_2: |
| 83 | case AMR_12_2: |
| 84 | return true; |
| 85 | default: |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
Mychaela N. Falconia | ec65085 | 2023-04-16 01:34:35 +0000 | [diff] [blame] | 90 | /* SID ternary classification per GSM 06.31 & 06.81 section 6.1.1 */ |
| 91 | enum osmo_gsm631_sid_class { |
| 92 | OSMO_GSM631_SID_CLASS_SPEECH = 0, |
| 93 | OSMO_GSM631_SID_CLASS_INVALID = 1, |
| 94 | OSMO_GSM631_SID_CLASS_VALID = 2, |
| 95 | }; |
| 96 | |
Harald Welte | 6789ba3 | 2017-05-31 02:47:43 +0200 | [diff] [blame] | 97 | bool osmo_fr_check_sid(const uint8_t *rtp_payload, size_t payload_len); |
| 98 | bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len); |
Mychaela N. Falconia | 7f91880 | 2023-03-24 00:46:12 +0000 | [diff] [blame] | 99 | bool osmo_efr_check_sid(const uint8_t *rtp_payload, size_t payload_len); |
Mychaela N. Falconia | ec65085 | 2023-04-16 01:34:35 +0000 | [diff] [blame] | 100 | |
| 101 | enum osmo_gsm631_sid_class osmo_fr_sid_classify(const uint8_t *rtp_payload); |
| 102 | enum osmo_gsm631_sid_class osmo_efr_sid_classify(const uint8_t *rtp_payload); |
Mychaela N. Falconia | 341c4ec | 2023-05-09 07:51:12 +0000 | [diff] [blame] | 103 | |
| 104 | /*! Check if given FR codec frame is any kind of SID, valid or invalid |
| 105 | * \param[in] rtp_payload Buffer with RTP payload |
| 106 | * \returns true if the frame is an "accepted SID frame" in GSM 06.31 |
| 107 | * definition, false otherwise. |
| 108 | */ |
| 109 | static inline bool osmo_fr_is_any_sid(const uint8_t *rtp_payload) |
| 110 | { |
| 111 | enum osmo_gsm631_sid_class sidc; |
| 112 | |
| 113 | sidc = osmo_fr_sid_classify(rtp_payload); |
| 114 | return sidc != OSMO_GSM631_SID_CLASS_SPEECH; |
| 115 | } |
| 116 | |
| 117 | /*! Check if given EFR codec frame is any kind of SID, valid or invalid |
| 118 | * \param[in] rtp_payload Buffer with RTP payload |
| 119 | * \returns true if the frame is an "accepted SID frame" in GSM 06.81 |
| 120 | * definition, false otherwise. |
| 121 | */ |
| 122 | static inline bool osmo_efr_is_any_sid(const uint8_t *rtp_payload) |
| 123 | { |
| 124 | enum osmo_gsm631_sid_class sidc; |
| 125 | |
| 126 | sidc = osmo_efr_sid_classify(rtp_payload); |
| 127 | return sidc != OSMO_GSM631_SID_CLASS_SPEECH; |
| 128 | } |
| 129 | |
Mychaela N. Falconia | 295636b | 2023-04-16 17:19:50 +0000 | [diff] [blame] | 130 | bool osmo_fr_sid_preen(uint8_t *rtp_payload); |
| 131 | bool osmo_efr_sid_preen(uint8_t *rtp_payload); |
Mychaela N. Falconia | ec65085 | 2023-04-16 01:34:35 +0000 | [diff] [blame] | 132 | |
Mychaela N. Falconia | 2974a23 | 2023-05-26 20:38:02 +0000 | [diff] [blame] | 133 | void osmo_fr_sid_reset(uint8_t *rtp_payload); |
Mychaela N. Falconia | 18e5af5 | 2023-05-26 21:15:06 +0000 | [diff] [blame] | 134 | void osmo_hr_sid_reset(uint8_t *rtp_payload); |
Mychaela N. Falconia | 2974a23 | 2023-05-26 20:38:02 +0000 | [diff] [blame] | 135 | void osmo_efr_sid_reset(uint8_t *rtp_payload); |
| 136 | |
Max | 92db150 | 2016-05-25 18:13:51 +0200 | [diff] [blame] | 137 | int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft, |
| 138 | enum osmo_amr_quality bfi); |
| 139 | int osmo_amr_rtp_dec(const uint8_t *payload, int payload_len, uint8_t *cmr, |
| 140 | int8_t *cmi, enum osmo_amr_type *ft, |
| 141 | enum osmo_amr_quality *bfi, int8_t *sti); |