libosmo-netif  1.4.0.19-26cd.202401052026
Osmocom network interface library
amr.h
1 #ifndef _OSMO_AMR_H_
2 #define _OSMO_AMR_H_
3 
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 
8 #include <osmocom/core/endian.h>
9 
10 /* As defined by RFC3267: Adaptive Multi-Rate (AMR) */
11 
12 /*
13  * +----------------+-------------------+----------------
14  * | payload header | table of contents | speech data ...
15  * +----------------+-------------------+----------------
16  */
17 
18 /*
19  * 4.3. Bandwidth-Efficient Mode:
20  *
21  * Summary from 4.3.4: Same as Octet aligned (see below) but without padding after header and ToC:
22  * 0 1
23  * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
24  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25  * | CMR |F| FT |Q|X X X X X X|
26  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27  *
28  * X means AMR payload (padding in case of FT=NO_DATA).
29  */
30 struct amr_hdr_bwe {
31 #if OSMO_IS_LITTLE_ENDIAN
32  uint8_t ft_hi:3, /* coding mode highest part */
33  f:1,
34  cmr:4; /* Codec Mode Request */
35  uint8_t data_start:6,
36  q:1, /* OK (not damaged) at origin? */
37  ft_lo:1; /* coding mode lowest bit */
38 #elif OSMO_IS_BIG_ENDIAN
39 /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */
40  uint8_t cmr:4, f:1, ft_hi:3;
41  uint8_t ft_lo:1, q:1, data_start:6;
42 #endif
43  uint8_t data[0];
44 } __attribute__((packed));
45 
46 /* See diagram above: CMR (4) + F (1) + FT (4) + Q (1) = 10 */
47 #define AMR_HDR_BWE_LEN_BITS 10
48 
49 /*
50  * 4.4. Octet-aligned Mode:
51  *
52  * 4.4.1. The Payload Header:
53  *
54  * 0 1 2 3 4 5 6 7
55  * +-+-+-+-+-+-+-+-+
56  * | CMR |X X X X|
57  * +-+-+-+-+-+-+-+-+
58  *
59  * According to: 3GPP TS 26.201 "AMR Wideband speech codec; Frame Structure",
60  * version 5.0.0 (2001-03), 3rd Generation Partnership Project (3GPP):
61  *
62  * Possible Frame type / CMR values:
63  *
64  * 0-8 for AMR-WB (from 6.60 kbit/s to 23.85 kbit/s)
65  * 9 (SID) confort noise.
66  * 10-13 future use.
67  * 14 means lost speech frame (only available for AMR-WB)
68  * 15 means no data
69  *
70  * 4.4.2. The table of contents:
71  *
72  * 0 1 2 3 4 5 6 7
73  * +-+-+-+-+-+-+-+-+
74  * |F| FT |Q|X X|
75  * +-+-+-+-+-+-+-+-+
76  *
77  * X means padding.
78  */
79 
80 struct amr_hdr {
81 #if OSMO_IS_LITTLE_ENDIAN
82  /* Payload Header */
83  uint8_t pad1:4,
84  cmr:4; /* Codec Mode Request */
85  /* Table of Contents */
86  uint8_t pad2:2,
87  q:1, /* OK (not damaged) at origin? */
88  ft:4, /* coding mode */
89  f:1; /* followed by another speech frame? */
90 #elif OSMO_IS_BIG_ENDIAN
91 /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */
92  uint8_t cmr:4, pad1:4;
93  uint8_t f:1, ft:4, q:1, pad2:2;
94 #endif
95  uint8_t data[0];
96 } __attribute__((packed));
97 
98 static inline void *osmo_amr_get_payload(struct amr_hdr *amrh)
99 {
100  return (uint8_t *)amrh + sizeof(struct amr_hdr);
101 }
102 
103 /* AMR voice frame type identifiers
104  * See also 3GPP TS 26.101, Table 1a: Interpretation of Frame Type, Mode
105  * Indication and Mode Request fields */
106 #define AMR_FT_0 0 /* 4.75 */
107 #define AMR_FT_1 1 /* 5.15 */
108 #define AMR_FT_2 2 /* 5.90 */
109 #define AMR_FT_3 3 /* 6.70 */
110 #define AMR_FT_4 4 /* 7.40 */
111 #define AMR_FT_5 5 /* 7.95 */
112 #define AMR_FT_6 6 /* 10.2 */
113 #define AMR_FT_7 7 /* 12.2 */
114 #define AMR_FT_SID 8 /* AMR SID */
115 #define AMR_FT_GSM_EFR_SID 9 /* GSM-EFR SID */
116 #define AMR_FT_TDMA_EFR_SID 10 /* TDMA-EFR SID */
117 #define AMR_FT_PDC_EFR_SID 11 /* PDC-EFR SID */
118 /* version 16.0.0 Release 16: 12-14 for future use */
119 #define AMR_FT_NO_DATA 15 /* NO_DATA */
120 #define AMR_FT_MAX 16 /* INTERNAL, NO NOT USE OUTSIDE libosmo-netif */
121 
122 /* AMR voice frame length (in bits).
123  * See also RFC 3267, chapter 3.6.
124  *
125  * NOTE: These constants refer to the length of one AMR speech frame-block,
126  * not counting CMR, TOC. */
127 #define AMR_FT_0_LEN_BITS 95 /* 4.75 */
128 #define AMR_FT_1_LEN_BITS 103 /* 5.15 */
129 #define AMR_FT_2_LEN_BITS 118 /* 5.90 */
130 #define AMR_FT_3_LEN_BITS 134 /* 6.70 */
131 #define AMR_FT_4_LEN_BITS 148 /* 7.40 */
132 #define AMR_FT_5_LEN_BITS 159 /* 7.95 */
133 #define AMR_FT_6_LEN_BITS 204 /* 10.2 */
134 #define AMR_FT_7_LEN_BITS 244 /* 12.2 */
135 #define AMR_FT_SID_LEN_BITS 39 /* SID */
136 #define AMR_FT_GSM_EFR_SID_LEN_BITS 43 /* GSM-EFR SID */
137 #define AMR_FT_TDMA_EFR_SID_LEN_BITS 38 /* TDMA-EFR SID */
138 #define AMR_FT_PDC_EFR_SID_LEN_BITS 37 /* PDC-EFR SID */
139 /* version 16.0.0 Release 16: 12-14 for future use */
140 #define AMR_FT_NO_DATA_LEN_BITS 0 /* NO_DATA */
141 
142 /* AMR voice frame length (in bytes, rounded).
143  *
144  * NOTE: These constants refer to the length of one AMR speech frame-block,
145  * not counting CMR, TOC. */
146 #define AMR_FT_0_LEN ((AMR_FT_0_LEN_BITS+7)/8) /* 4.75 */
147 #define AMR_FT_1_LEN ((AMR_FT_1_LEN_BITS+7)/8) /* 5.15 */
148 #define AMR_FT_2_LEN ((AMR_FT_2_LEN_BITS+7)/8) /* 5.90 */
149 #define AMR_FT_3_LEN ((AMR_FT_3_LEN_BITS+7)/8) /* 6.70 */
150 #define AMR_FT_4_LEN ((AMR_FT_4_LEN_BITS+7)/8) /* 7.40 */
151 #define AMR_FT_5_LEN ((AMR_FT_5_LEN_BITS+7)/8) /* 7.95 */
152 #define AMR_FT_6_LEN ((AMR_FT_6_LEN_BITS+7)/8) /* 10.2 */
153 #define AMR_FT_7_LEN ((AMR_FT_7_LEN_BITS+7)/8) /* 12.2 */
154 #define AMR_FT_SID_LEN ((AMR_FT_SID_LEN_BITS+7)/8) /* SID */
155 #define AMR_FT_GSM_EFR_SID_LEN ((AMR_FT_GSM_EFR_SID_LEN_BITS+7)/8) /* GSM-EFR SID */
156 #define AMR_FT_TDMA_EFR_SID_LEN ((AMR_FT_TDMA_EFR_SID_LEN_BITS+7)/8) /* TDMA-EFR SID */
157 #define AMR_FT_PDC_EFR_SID_LEN ((AMR_FT_PDC_EFR_SID_LEN_BITS+7)/8) /* PDC-EFR SID */
158 /* version 16.0.0 Release 16: 12-14 for future use */
159 #define AMR_FT_NO_DATA_LEN ((AMR_FT_NO_DATA_LEN_BITS+7)/8) /* NO_DATA */
160 
161 int osmo_amr_ft_valid(uint8_t amr_ft);
162 size_t osmo_amr_bytes(uint8_t amr_cmr);
163 size_t osmo_amr_bits(uint8_t amr_ft);
164 
165 bool osmo_amr_is_oa(const uint8_t *payload, unsigned int payload_len);
166 int osmo_amr_oa_to_bwe(uint8_t *payload, unsigned int payload_len);
167 int osmo_amr_bwe_to_oa(uint8_t *payload, unsigned int payload_len,
168  unsigned int payload_maxlen);
169 int osmo_amr_bwe_to_iuup(uint8_t *payload, unsigned int payload_len);
170 int osmo_amr_iuup_to_bwe(uint8_t *payload, unsigned int payload_len,
171  unsigned int payload_maxlen);
172 int osmo_amr_bytes_to_ft(size_t bytes);
173 
174 #endif
Definition: amr.h:30
Definition: amr.h:80