libosmocodec 1.13.1.1-afb4 Osmocom codec library

codec.h
Go to the documentation of this file.
1
3#pragma once
4
5#include <stdint.h>
6#include <stdbool.h>
7
8#include <osmocom/core/utils.h>
9#include <osmocom/core/bits.h>
10
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
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
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 */
26
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 */
35
36 extern const uint8_t osmo_gsm611_silence_frame[GSM_FR_BYTES];
37 extern const uint8_t osmo_gsm620_silence_frame[GSM_HR_BYTES];
38 extern const uint8_t osmo_gsm620_homing_frame[GSM_HR_BYTES];
39 extern const uint8_t osmo_gsm660_homing_frame[GSM_EFR_BYTES];
40
41 extern const struct value_string osmo_amr_type_names[];
42
43 enum osmo_amr_type {
44 AMR_4_75 = 0,
45 AMR_5_15 = 1,
46 AMR_5_90 = 2,
47 AMR_6_70 = 3,
48 AMR_7_40 = 4,
49 AMR_7_95 = 5,
50 AMR_10_2 = 6,
51 AMR_12_2 = 7,
52 AMR_SID = 8,
53 AMR_GSM_EFR_SID = 9,
54 AMR_TDMA_EFR_SID = 10,
55 AMR_PDC_EFR_SID = 11,
56 AMR_NO_DATA = 15,
57};
58
59 static inline const char *osmo_amr_type_name(enum osmo_amr_type type)
60{ return get_value_string(osmo_amr_type_names, type); }
61
62 enum osmo_amr_quality {
63 AMR_BAD = 0,
64 AMR_GOOD = 1
65};
66
67 extern const uint8_t gsm690_bitlength[AMR_NO_DATA+1];
68
69 int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
70 int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode);
71
76 static inline bool osmo_amr_is_speech(enum osmo_amr_type ft)
77{
78 switch (ft) {
79 case AMR_4_75:
80 case AMR_5_15:
81 case AMR_5_90:
82 case AMR_6_70:
83 case AMR_7_40:
84 case AMR_7_95:
85 case AMR_10_2:
86 case AMR_12_2:
87 return true;
88 default:
89 return false;
90 }
91}
92
93 extern const uint8_t osmo_amr_sbits_per_mode[AMR_NO_DATA+1];
94 extern const uint8_t osmo_amr_params_per_mode[AMR_NO_DATA+1];
95
96 int osmo_amr_param_to_sbits(ubit_t *s_bits, const uint16_t *param,
97 enum osmo_amr_type mode);
98 int osmo_amr_sbits_to_param(uint16_t *param, const ubit_t *s_bits,
99 enum osmo_amr_type mode);
100
101 extern const uint16_t osmo_amr_dhf_4_75[];
102 extern const uint16_t osmo_amr_dhf_5_15[];
103 extern const uint16_t osmo_amr_dhf_5_90[];
104 extern const uint16_t osmo_amr_dhf_6_70[];
105 extern const uint16_t osmo_amr_dhf_7_40[];
106 extern const uint16_t osmo_amr_dhf_7_95[];
107 extern const uint16_t osmo_amr_dhf_10_2[];
108 extern const uint16_t osmo_amr_dhf_12_2[];
109
110/* SID ternary classification per GSM 06.31 & 06.81 section 6.1.1 */
111 enum osmo_gsm631_sid_class {
112 OSMO_GSM631_SID_CLASS_SPEECH = 0,
113 OSMO_GSM631_SID_CLASS_INVALID = 1,
114 OSMO_GSM631_SID_CLASS_VALID = 2,
115};
116
117 bool osmo_fr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
118 bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
119 bool osmo_efr_check_sid(const uint8_t *rtp_payload, size_t payload_len);
120
121 enum osmo_gsm631_sid_class osmo_fr_sid_classify(const uint8_t *rtp_payload);
122 enum osmo_gsm631_sid_class osmo_efr_sid_classify(const uint8_t *rtp_payload);
123 enum osmo_gsm631_sid_class osmo_hr_sid_classify(const uint8_t *rtp_payload,
124 bool bci_flag,
125 bool *bfi_from_bci);
126
132 static inline bool osmo_fr_is_any_sid(const uint8_t *rtp_payload)
133{
134 enum osmo_gsm631_sid_class sidc;
135
136 sidc = osmo_fr_sid_classify(rtp_payload);
137 return sidc != OSMO_GSM631_SID_CLASS_SPEECH;
138}
139
145 static inline bool osmo_efr_is_any_sid(const uint8_t *rtp_payload)
146{
147 enum osmo_gsm631_sid_class sidc;
148
149 sidc = osmo_efr_sid_classify(rtp_payload);
150 return sidc != OSMO_GSM631_SID_CLASS_SPEECH;
151}
152
153 bool osmo_fr_sid_preen(uint8_t *rtp_payload);
154 bool osmo_efr_sid_preen(uint8_t *rtp_payload);
155
156 void osmo_fr_sid_reset(uint8_t *rtp_payload);
157 void osmo_hr_sid_reset(uint8_t *rtp_payload);
158 void osmo_efr_sid_reset(uint8_t *rtp_payload);
159
160 int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft,
161 enum osmo_amr_quality bfi);
162 int osmo_amr_rtp_dec(const uint8_t *payload, int payload_len, uint8_t *cmr,
163 int8_t *cmi, enum osmo_amr_type *ft,
164 enum osmo_amr_quality *bfi, int8_t *sti);
bits.h
osmo_amr_param_to_sbits
int osmo_amr_param_to_sbits(ubit_t *s_bits, const uint16_t *param, enum osmo_amr_type mode)
Convert AMR codec frame from parameters to s-bits.
Definition: gsm690.c:457
osmo_fr_is_any_sid
static bool osmo_fr_is_any_sid(const uint8_t *rtp_payload)
Check if given FR codec frame is any kind of SID, valid or invalid.
Definition: codec.h:132
osmo_hr_sid_classify
enum osmo_gsm631_sid_class osmo_hr_sid_classify(const uint8_t *rtp_payload, bool bci_flag, bool *bfi_from_bci)
Definition: hr_sid_class.c:115
gsm690_10_2_bitorder
const uint16_t gsm690_10_2_bitorder[]
Definition: gsm690.c:73
osmo_amr_params_per_mode
const uint8_t osmo_amr_params_per_mode[AMR_NO_DATA+1]
This table provides the number of distinct codec parameters (groupings of s-bits into 16-bit paramete...
Definition: gsm690.c:355
GSM_EFR_BYTES
#define GSM_EFR_BYTES
Definition: codec.h:16
osmo_gsm620_homing_frame
const uint8_t osmo_gsm620_homing_frame[GSM_HR_BYTES]
Definition: gsm620.c:292
osmo_fr_check_sid
bool osmo_fr_check_sid(const uint8_t *rtp_payload, size_t payload_len)
Check whether RTP frame contains FR SID code word according to TS 101 318 §5.1.2.
Definition: gsm610.c:334
osmo_fr_sid_preen
bool osmo_fr_sid_preen(uint8_t *rtp_payload)
Preen potentially-SID FR codec frame in RTP format, ensuring that it is either a speech frame or a va...
Definition: gsm610.c:442
osmo_amr_dhf_5_15
const uint16_t osmo_amr_dhf_5_15[]
Definition: gsm690.c:532
osmo_amr_sbits_per_mode
const uint8_t osmo_amr_sbits_per_mode[AMR_NO_DATA+1]
This table provides the number of s-bits (as defined in TS 26.090 and TS 26.092, clause 7 in each spe...
Definition: gsm690.c:335
osmo_amr_sbits_to_param
int osmo_amr_sbits_to_param(uint16_t *param, const ubit_t *s_bits, enum osmo_amr_type mode)
Convert AMR codec frame from s-bits to parameters.
Definition: gsm690.c:490
osmo_amr_dhf_7_40
const uint16_t osmo_amr_dhf_7_40[]
Definition: gsm690.c:550
osmo_efr_sid_reset
void osmo_efr_sid_reset(uint8_t *rtp_payload)
Reset the SID field of a potentially corrupted, but still valid GSM-EFR SID frame in RTP encoding to ...
Definition: gsm660.c:386
gsm690_bitlength
const uint8_t gsm690_bitlength[AMR_NO_DATA+1]
These constants refer to the length of one "AMR core frame" as per TS 26.101 Section 4....
Definition: gsm690.c:219
GSM_HR_BYTES
#define GSM_HR_BYTES
Definition: codec.h:14
osmo_efr_sid_classify
enum osmo_gsm631_sid_class osmo_efr_sid_classify(const uint8_t *rtp_payload)
Classify potentially-SID EFR codec frame in RTP format according to the rules of GSM 06....
Definition: gsm660.c:348
gsm690_7_4_bitorder
const uint16_t gsm690_7_4_bitorder[]
Definition: gsm690.c:124
osmo_gsm611_silence_frame
const uint8_t osmo_gsm611_silence_frame[GSM_FR_BYTES]
Definition: gsm610.c:305
osmo_efr_sid_preen
bool osmo_efr_sid_preen(uint8_t *rtp_payload)
Preen potentially-SID EFR codec frame in RTP format, ensuring that it is either a speech frame or a v...
Definition: gsm660.c:412
osmo_amr_dhf_12_2
const uint16_t osmo_amr_dhf_12_2[]
Definition: gsm690.c:570
osmo_gsm620_silence_frame
const uint8_t osmo_gsm620_silence_frame[GSM_HR_BYTES]
Definition: gsm620.c:278
gsm690_5_9_bitorder
const uint16_t gsm690_5_9_bitorder[]
Definition: gsm690.c:167
gsm690_4_75_bitorder
const uint16_t gsm690_4_75_bitorder[]
Definition: gsm690.c:204
osmo_fr_sid_classify
enum osmo_gsm631_sid_class osmo_fr_sid_classify(const uint8_t *rtp_payload)
Classify potentially-SID FR codec frame in RTP format according to the rules of GSM 06....
Definition: gsm610.c:385
GSM_FR_BYTES
#define GSM_FR_BYTES
Definition: codec.h:12
gsm690_5_15_bitorder
const uint16_t gsm690_5_15_bitorder[]
Definition: gsm690.c:186
osmo_amr_dhf_6_70
const uint16_t osmo_amr_dhf_6_70[]
Definition: gsm690.c:544
osmo_amr_type_name
static const char * osmo_amr_type_name(enum osmo_amr_type type)
Definition: codec.h:59
osmo_amr_d_to_s
int osmo_amr_d_to_s(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode)
Convert from d-bits to s-bits (codec input).
Definition: gsm690.c:304
osmo_fr_sid_reset
void osmo_fr_sid_reset(uint8_t *rtp_payload)
Reset the SID field and the unused bits of a potentially corrupted, but still valid GSM-FR SID frame ...
Definition: gsm610.c:420
osmo_amr_rtp_enc
int osmo_amr_rtp_enc(uint8_t *payload, uint8_t cmr, enum osmo_amr_type ft, enum osmo_amr_quality bfi)
Encode various AMR parameters from RTP payload (RFC 4867)
Definition: gsm690.c:664
osmo_gsm660_homing_frame
const uint8_t osmo_gsm660_homing_frame[GSM_EFR_BYTES]
Definition: gsm660.c:269
osmo_amr_type
osmo_amr_type
Definition: codec.h:43
AMR_NO_DATA
@ AMR_NO_DATA
Definition: codec.h:56
AMR_4_75
@ AMR_4_75
Definition: codec.h:44
AMR_12_2
@ AMR_12_2
Definition: codec.h:51
AMR_7_40
@ AMR_7_40
Definition: codec.h:48
AMR_10_2
@ AMR_10_2
Definition: codec.h:50
AMR_6_70
@ AMR_6_70
Definition: codec.h:47
AMR_GSM_EFR_SID
@ AMR_GSM_EFR_SID
Definition: codec.h:53
AMR_PDC_EFR_SID
@ AMR_PDC_EFR_SID
Definition: codec.h:55
AMR_SID
@ AMR_SID
Definition: codec.h:52
AMR_5_90
@ AMR_5_90
Definition: codec.h:46
AMR_5_15
@ AMR_5_15
Definition: codec.h:45
AMR_7_95
@ AMR_7_95
Definition: codec.h:49
AMR_TDMA_EFR_SID
@ AMR_TDMA_EFR_SID
Definition: codec.h:54
osmo_hr_sid_reset
void osmo_hr_sid_reset(uint8_t *rtp_payload)
Reset the SID field of a potentially corrupted, but still valid GSM-HR SID frame in TS 101 318 format...
Definition: gsm620.c:351
osmo_amr_is_speech
static bool osmo_amr_is_speech(enum osmo_amr_type ft)
Check if given AMR Frame Type is a speech frame.
Definition: codec.h:76
osmo_amr_rtp_dec
int osmo_amr_rtp_dec(const uint8_t *payload, int payload_len, uint8_t *cmr, int8_t *cmi, enum osmo_amr_type *ft, enum osmo_amr_quality *bfi, int8_t *sti)
Decode various AMR parameters from RTP payload (RFC 4867) acording to 3GPP TS 26.101.
Definition: gsm690.c:616
osmo_gsm631_sid_class
osmo_gsm631_sid_class
Definition: codec.h:111
OSMO_GSM631_SID_CLASS_SPEECH
@ OSMO_GSM631_SID_CLASS_SPEECH
Definition: codec.h:112
OSMO_GSM631_SID_CLASS_VALID
@ OSMO_GSM631_SID_CLASS_VALID
Definition: codec.h:114
OSMO_GSM631_SID_CLASS_INVALID
@ OSMO_GSM631_SID_CLASS_INVALID
Definition: codec.h:113
osmo_amr_type_names
const struct value_string osmo_amr_type_names[]
Definition: gsm690.c:586
gsm690_7_95_bitorder
const uint16_t gsm690_7_95_bitorder[]
Definition: gsm690.c:101
osmo_amr_s_to_d
int osmo_amr_s_to_d(ubit_t *out, const ubit_t *in, uint16_t n_bits, enum osmo_amr_type amr_mode)
Convert from S-bits (codec output) to d-bits.
Definition: gsm690.c:278
osmo_amr_dhf_4_75
const uint16_t osmo_amr_dhf_4_75[]
Definition: gsm690.c:526
gsm620_unvoiced_bitorder
const uint16_t gsm620_unvoiced_bitorder[]
Definition: gsm620.c:32
osmo_amr_dhf_7_95
const uint16_t osmo_amr_dhf_7_95[]
Definition: gsm690.c:556
gsm610_bitorder
const uint16_t gsm610_bitorder[]
Definition: gsm610.c:36
osmo_efr_is_any_sid
static bool osmo_efr_is_any_sid(const uint8_t *rtp_payload)
Check if given EFR codec frame is any kind of SID, valid or invalid.
Definition: codec.h:145
gsm690_12_2_bitorder
const uint16_t gsm690_12_2_bitorder[]
Definition: gsm690.c:41
osmo_amr_dhf_10_2
const uint16_t osmo_amr_dhf_10_2[]
Definition: gsm690.c:562
gsm690_6_7_bitorder
const uint16_t gsm690_6_7_bitorder[]
Definition: gsm690.c:146
osmo_amr_quality
osmo_amr_quality
Definition: codec.h:62
AMR_BAD
@ AMR_BAD
Definition: codec.h:63
AMR_GOOD
@ AMR_GOOD
Definition: codec.h:64
osmo_hr_check_sid
bool osmo_hr_check_sid(const uint8_t *rtp_payload, size_t payload_len)
Check whether RTP frame contains HR SID code word according to TS 101 318 §5.2.2.
Definition: gsm620.c:309
osmo_efr_check_sid
bool osmo_efr_check_sid(const uint8_t *rtp_payload, size_t payload_len)
Check whether RTP frame contains EFR SID code word according to TS 101 318 §5.3.2.
Definition: gsm660.c:297
osmo_amr_dhf_5_90
const uint16_t osmo_amr_dhf_5_90[]
Definition: gsm690.c:538
gsm620_voiced_bitorder
const uint16_t gsm620_voiced_bitorder[]
Definition: gsm620.c:151
gsm660_bitorder
const uint16_t gsm660_bitorder[]
Definition: gsm660.c:38
type
enum osmo_sub_auth_type type
ubit_t
uint8_t ubit_t
get_value_string
const char * get_value_string(const struct value_string *vs, uint32_t val)
value_string
utils.h