libosmo-netif  1.2.0.67-9ee4
Osmocom network interface library
amr.h
1 #ifndef _OSMO_AMR_H_
2 #define _OSMO_AMR_H_
3 
4 #include <osmocom/core/endian.h>
5 
6 /* As defined by RFC3267: Adaptive Multi-Rate (AMR) */
7 
8 /*
9  * +----------------+-------------------+----------------
10  * | payload header | table of contents | speech data ...
11  * +----------------+-------------------+----------------
12  */
13 
14 /*
15  * 4.4. Octet-aligned Mode:
16  *
17  * 4.4.1. The Payload Header:
18  *
19  * 0 1 2 3 4 5 6 7
20  * +-+-+-+-+-+-+-+-+
21  * | CMR |X X X X|
22  * +-+-+-+-+-+-+-+-+
23  *
24  * According to: 3GPP TS 26.201 "AMR Wideband speech codec; Frame Structure",
25  * version 5.0.0 (2001-03), 3rd Generation Partnership Project (3GPP):
26  *
27  * Possible Frame type / CMR values:
28  *
29  * 0-8 for AMR-WB (from 6.60 kbit/s to 23.85 kbit/s)
30  * 9 (SID) confort noise.
31  * 10-13 future use.
32  * 14 means lost speech frame (only available for AMR-WB)
33  * 15 means no data
34  *
35  * 4.4.2. The table of contents:
36  *
37  * 0 1 2 3 4 5 6 7
38  * +-+-+-+-+-+-+-+-+
39  * |F| FT |Q|X X|
40  * +-+-+-+-+-+-+-+-+
41  *
42  * X means padding.
43  */
44 
45 struct amr_hdr {
46 #if OSMO_IS_LITTLE_ENDIAN
47  /* Payload Header */
48  uint8_t pad1:4,
49  cmr:4; /* Codec Mode Request */
50  /* Table of Contents */
51  uint8_t pad2:2,
52  q:1, /* OK (not damaged) at origin? */
53  ft:4, /* coding mode */
54  f:1; /* followed by another speech frame? */
55 #elif OSMO_IS_BIG_ENDIAN
56 /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
57  uint8_t cmr:4, pad1:4;
58  uint8_t f:1, ft:4, q:1, pad2:2;
59 #endif
60  uint8_t data[0];
61 } __attribute__((packed));
62 
63 static inline void *osmo_amr_get_payload(struct amr_hdr *amrh)
64 {
65  return (uint8_t *)amrh + sizeof(struct amr_hdr);
66 }
67 
68 /* AMR voice frame type identifiers
69  * See also 3GPP TS 26.101, Table 1a: Interpretation of Frame Type, Mode
70  * Indication and Mode Request fields */
71 #define AMR_FT_0 0 /* 4.75 */
72 #define AMR_FT_1 1 /* 5.15 */
73 #define AMR_FT_2 2 /* 5.90 */
74 #define AMR_FT_3 3 /* 6.70 */
75 #define AMR_FT_4 4 /* 7.40 */
76 #define AMR_FT_5 5 /* 7.95 */
77 #define AMR_FT_6 6 /* 10.2 */
78 #define AMR_FT_7 7 /* 12.2 */
79 #define AMR_FT_SID 8 /* SID */
80 #define AMR_FT_MAX 9
81 
82 /* AMR voice frame length (in bits).
83  * See also RFC 3267, chapter 3.6.
84  *
85  * NOTE: These constants refer to the length of one AMR speech frame-block,
86  * not counting CMR, TOC. */
87 #define AMR_FT_0_LEN_BITS 95 /* 4.75 */
88 #define AMR_FT_1_LEN_BITS 103 /* 5.15 */
89 #define AMR_FT_2_LEN_BITS 118 /* 5.90 */
90 #define AMR_FT_3_LEN_BITS 134 /* 6.70 */
91 #define AMR_FT_4_LEN_BITS 148 /* 7.40 */
92 #define AMR_FT_5_LEN_BITS 159 /* 7.95 */
93 #define AMR_FT_6_LEN_BITS 204 /* 10.2 */
94 #define AMR_FT_7_LEN_BITS 244 /* 12.2 */
95 #define AMR_FT_SID_LEN_BITS 39 /* SID */
96 
97 /* AMR voice frame length (in bytes, rounded).
98  *
99  * NOTE: These constants refer to the length of one AMR speech frame-block,
100  * not counting CMR, TOC. */
101 #define AMR_FT_0_LEN ((AMR_FT_0_LEN_BITS+7)/8) /* 4.75 */
102 #define AMR_FT_1_LEN ((AMR_FT_1_LEN_BITS+7)/8) /* 5.15 */
103 #define AMR_FT_2_LEN ((AMR_FT_2_LEN_BITS+7)/8) /* 5.90 */
104 #define AMR_FT_3_LEN ((AMR_FT_3_LEN_BITS+7)/8) /* 6.70 */
105 #define AMR_FT_4_LEN ((AMR_FT_4_LEN_BITS+7)/8) /* 7.40 */
106 #define AMR_FT_5_LEN ((AMR_FT_5_LEN_BITS+7)/8) /* 7.95 */
107 #define AMR_FT_6_LEN ((AMR_FT_6_LEN_BITS+7)/8) /* 10.2 */
108 #define AMR_FT_7_LEN ((AMR_FT_7_LEN_BITS+7)/8) /* 12.2 */
109 #define AMR_FT_SID_LEN ((AMR_FT_SID_LEN_BITS+7)/8) /* SID */
110 
111 int osmo_amr_ft_valid(uint8_t amr_ft);
112 size_t osmo_amr_bytes(uint8_t amr_cmr);
113 size_t osmo_amr_bits(uint8_t amr_ft);
114 
115 bool osmo_amr_is_oa(uint8_t *payload, unsigned int payload_len);
116 int osmo_amr_oa_to_bwe(uint8_t *payload, unsigned int payload_len);
117 int osmo_amr_bwe_to_oa(uint8_t *payload, unsigned int payload_len,
118  unsigned int payload_maxlen);
119 int osmo_amr_bwe_to_iuup(uint8_t *payload, unsigned int payload_len);
120 int osmo_amr_iuup_to_bwe(uint8_t *payload, unsigned int payload_len,
121  unsigned int payload_maxlen);
122 int osmo_amr_bytes_to_ft(size_t bytes);
123 
124 #endif
Definition: amr.h:45