blob: 731f16967f3b7e0568cf3254dcf6d232ff88265b [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
Mychaela N. Falconiaf86ec502023-05-10 18:18:42 +000036extern const uint8_t osmo_gsm611_silence_frame[GSM_FR_BYTES];
37
Max92db1502016-05-25 18:13:51 +020038extern const struct value_string osmo_amr_type_names[];
39
40enum 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 Maier2bd4bc92021-08-23 15:42:51 +020056static inline const char *osmo_amr_type_name(enum osmo_amr_type type)
57{ return get_value_string(osmo_amr_type_names, type); }
58
Max92db1502016-05-25 18:13:51 +020059enum osmo_amr_quality {
60 AMR_BAD = 0,
61 AMR_GOOD = 1
62};
63
Harald Welte5785a4a2020-05-13 23:10:31 +020064extern const uint8_t gsm690_bitlength[AMR_NO_DATA+1];
65
66int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
67int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
68
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069/*! Check if given AMR Frame Type is a speech frame
Max74a8f082016-09-27 13:51:51 +020070 * \param[in] ft AMR Frame Type
71 * \returns true if AMR with given Frame Type contains voice, false otherwise
72 */
Max2bb65be2016-09-28 15:38:55 +020073static inline bool osmo_amr_is_speech(enum osmo_amr_type ft)
Max74a8f082016-09-27 13:51:51 +020074{
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. Falconiaec650852023-04-16 01:34:35 +000090/* SID ternary classification per GSM 06.31 & 06.81 section 6.1.1 */
91enum 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 Welte6789ba32017-05-31 02:47:43 +020097bool osmo_fr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
98bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
Mychaela N. Falconia7f918802023-03-24 00:46:12 +000099bool osmo_efr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
Mychaela N. Falconiaec650852023-04-16 01:34:35 +0000100
101enum osmo_gsm631_sid_class osmo_fr_sid_classify(const uint8_t *rtp_payload);
102enum osmo_gsm631_sid_class osmo_efr_sid_classify(const uint8_t *rtp_payload);
Mychaela N. Falconia341c4ec2023-05-09 07:51:12 +0000103
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 */
109static 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 */
122static 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. Falconia295636b2023-04-16 17:19:50 +0000130bool osmo_fr_sid_preen(uint8_t *rtp_payload);
131bool osmo_efr_sid_preen(uint8_t *rtp_payload);
Mychaela N. Falconiaec650852023-04-16 01:34:35 +0000132
Max92db1502016-05-25 18:13:51 +0200133int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft,
134 enum osmo_amr_quality bfi);
135int osmo_amr_rtp_dec(const uint8_t *payload, int payload_len, uint8_t *cmr,
136 int8_t *cmi, enum osmo_amr_type *ft,
137 enum osmo_amr_quality *bfi, int8_t *sti);