libosmo-netif 1.2.0
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
45struct 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} __attribute__((packed));
61
62static inline void *osmo_amr_get_payload(struct amr_hdr *amrh)
63{
64 return (uint8_t *)amrh + sizeof(struct amr_hdr);
65}
66
67/* AMR voice frame type identifiers
68 * See also 3GPP TS 26.101, Table 1a: Interpretation of Frame Type, Mode
69 * Indication and Mode Request fields */
70#define AMR_FT_0 0 /* 4.75 */
71#define AMR_FT_1 1 /* 5.15 */
72#define AMR_FT_2 2 /* 5.90 */
73#define AMR_FT_3 3 /* 6.70 */
74#define AMR_FT_4 4 /* 7.40 */
75#define AMR_FT_5 5 /* 7.95 */
76#define AMR_FT_6 6 /* 10.2 */
77#define AMR_FT_7 7 /* 12.2 */
78#define AMR_FT_SID 8 /* SID */
79#define AMR_FT_MAX 9
80
81/* AMR voice frame length (in bits).
82 * See also RFC 3267, chapter 3.6.
83 *
84 * NOTE: These constants refer to the length of one AMR speech frame-block,
85 * not counting CMR, TOC. */
86#define AMR_FT_0_LEN_BITS 95 /* 4.75 */
87#define AMR_FT_1_LEN_BITS 103 /* 5.15 */
88#define AMR_FT_2_LEN_BITS 118 /* 5.90 */
89#define AMR_FT_3_LEN_BITS 134 /* 6.70 */
90#define AMR_FT_4_LEN_BITS 148 /* 7.40 */
91#define AMR_FT_5_LEN_BITS 159 /* 7.95 */
92#define AMR_FT_6_LEN_BITS 204 /* 10.2 */
93#define AMR_FT_7_LEN_BITS 244 /* 12.2 */
94#define AMR_FT_SID_LEN_BITS 39 /* SID */
95
96/* AMR voice frame length (in bytes, rounded).
97 *
98 * NOTE: These constants refer to the length of one AMR speech frame-block,
99 * not counting CMR, TOC. */
100#define AMR_FT_0_LEN ((AMR_FT_0_LEN_BITS+7)/8) /* 4.75 */
101#define AMR_FT_1_LEN ((AMR_FT_1_LEN_BITS+7)/8) /* 5.15 */
102#define AMR_FT_2_LEN ((AMR_FT_2_LEN_BITS+7)/8) /* 5.90 */
103#define AMR_FT_3_LEN ((AMR_FT_3_LEN_BITS+7)/8) /* 6.70 */
104#define AMR_FT_4_LEN ((AMR_FT_4_LEN_BITS+7)/8) /* 7.40 */
105#define AMR_FT_5_LEN ((AMR_FT_5_LEN_BITS+7)/8) /* 7.95 */
106#define AMR_FT_6_LEN ((AMR_FT_6_LEN_BITS+7)/8) /* 10.2 */
107#define AMR_FT_7_LEN ((AMR_FT_7_LEN_BITS+7)/8) /* 12.2 */
108#define AMR_FT_SID_LEN ((AMR_FT_SID_LEN_BITS+7)/8) /* SID */
109
110int osmo_amr_ft_valid(uint8_t amr_ft);
111size_t osmo_amr_bytes(uint8_t amr_cmr);
112size_t osmo_amr_bits(uint8_t amr_ft);
113
114bool osmo_amr_is_oa(uint8_t *payload, unsigned int payload_len);
115int osmo_amr_oa_to_bwe(uint8_t *payload, unsigned int payload_len);
116int osmo_amr_bwe_to_oa(uint8_t *payload, unsigned int payload_len,
117 unsigned int payload_maxlen);
118int osmo_amr_bwe_to_iuup(uint8_t *payload, unsigned int payload_len);
119int osmo_amr_iuup_to_bwe(uint8_t *payload, unsigned int payload_len,
120 unsigned int payload_maxlen);
121int osmo_amr_bytes_to_ft(size_t bytes);
122
123#endif
Definition: amr.h:45