1 /**
2  * @file
3  * @brief Bluetooth LC3 codec handling
4  */
5 
6 /*
7  * Copyright (c) 2020 Intel Corporation
8  * Copyright (c) 2022-2024 Nordic Semiconductor ASA
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_LC3_H_
13 #define ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_LC3_H_
14 
15 /**
16  * @brief LC3
17  * @defgroup bt_lc3 Bluetooth LC3 codec
18 
19  * @since 3.0
20  * @version 0.8.0
21 
22  * @ingroup bluetooth
23  * @{
24  */
25 
26 #include <zephyr/sys/util_macro.h>
27 #include <zephyr/bluetooth/byteorder.h>
28 #include <zephyr/bluetooth/hci_types.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * @brief Helper to declare LC3 codec capability
36  *
37  * @p _max_frames_per_sdu is optional and will be included only if != 1
38  *
39  * @ref COND_CODE_1 is used to omit an LTV entry in case the @p _frames_per_sdu is 1.
40  * @ref COND_CODE_1 will evaluate to second argument if the flag parameter(first argument) is 1
41  * - removing one layer of paranteses.
42  * If the flags argument is != 1 it will evaluate to the third argument which inserts a LTV
43  * entry for the max_frames_per_sdu value.
44 
45  * @param _freq Supported Sampling Frequencies bitfield (see ``BT_AUDIO_CODEC_CAP_FREQ_*``)
46  * @param _duration Supported Frame Durations bitfield (see ``BT_AUDIO_CODEC_CAP_DURATION_*``)
47  * @param _chan_count Supported channels (see @ref BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT)
48  * @param _len_min Minimum number of octets supported per codec frame
49  * @param _len_max Maximum number of octets supported per codec frame
50  * @param _max_frames_per_sdu Supported maximum codec frames per SDU
51  */
52 #define BT_AUDIO_CODEC_CAP_LC3_DATA(_freq, _duration, _chan_count, _len_min, _len_max,             \
53 				    _max_frames_per_sdu)                                           \
54 	{                                                                                          \
55 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_FREQ, BT_BYTES_LIST_LE16(_freq)),      \
56 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_DURATION, (_duration)),                \
57 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_CHAN_COUNT, (_chan_count)),            \
58 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_FRAME_LEN,                             \
59 				    BT_BYTES_LIST_LE16(_len_min),                                  \
60 				    BT_BYTES_LIST_LE16(_len_max)),                                 \
61 		COND_CODE_1(_max_frames_per_sdu, (),                                               \
62 			    (BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_FRAME_COUNT,              \
63 						 (_max_frames_per_sdu))))                          \
64 	}
65 
66 /**
67  * @brief Helper to declare LC3 codec metadata
68  *
69  * @param _prefer_context Preferred contexts (@ref bt_audio_context)
70  */
71 #define BT_AUDIO_CODEC_CAP_LC3_META(_prefer_context)                                               \
72 	{                                                                                          \
73 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,                           \
74 				    BT_BYTES_LIST_LE16(_prefer_context))                           \
75 	}
76 
77 /**
78  * @brief Helper to declare LC3 codec
79  *
80  * @param _freq Supported Sampling Frequencies bitfield (see ``BT_AUDIO_CODEC_CAP_FREQ_*``)
81  * @param _duration Supported Frame Durations bitfield (see ``BT_AUDIO_CODEC_CAP_DURATION_*``)
82  * @param _chan_count Supported channels (see @ref BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT)
83  * @param _len_min Minimum number of octets supported per codec frame
84  * @param _len_max Maximum number of octets supported per codec frame
85  * @param _max_frames_per_sdu Supported maximum codec frames per SDU
86  * @param _prefer_context Preferred contexts (@ref bt_audio_context)
87  */
88 #define BT_AUDIO_CODEC_CAP_LC3(_freq, _duration, _chan_count, _len_min, _len_max,                  \
89 			       _max_frames_per_sdu, _prefer_context)                               \
90 	BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,                               \
91 			   BT_AUDIO_CODEC_CAP_LC3_DATA(_freq, _duration, _chan_count, _len_min,    \
92 						       _len_max, _max_frames_per_sdu),             \
93 			   BT_AUDIO_CODEC_CAP_LC3_META(_prefer_context))
94 
95 /**
96  * @brief Helper to declare LC3 codec data configuration
97  *
98  * @param _freq            Sampling frequency (``BT_AUDIO_CODEC_CFG_FREQ_*``)
99  * @param _duration        Frame duration (``BT_AUDIO_CODEC_CFG_DURATION_*``)
100  * @param _loc             Audio channel location bitfield (@ref bt_audio_location)
101  * @param _len             Octets per frame (16-bit integer)
102  * @param _frames_per_sdu  Frames per SDU (8-bit integer). This value is optional and will be
103  *                         included only if != 1
104  */
105 #define BT_AUDIO_CODEC_CFG_LC3_DATA(_freq, _duration, _loc, _len, _frames_per_sdu)                 \
106 	{                                                                                          \
107 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ, (_freq)),                             \
108 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_DURATION, (_duration)),                     \
109 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_CHAN_ALLOC, BT_BYTES_LIST_LE32(_loc)),      \
110 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FRAME_LEN, BT_BYTES_LIST_LE16(_len)),       \
111 		COND_CODE_1(_frames_per_sdu, (),                                                   \
112 			    (BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FRAME_BLKS_PER_SDU,            \
113 						 (_frames_per_sdu))))                              \
114 	}
115 
116 /** @brief Helper to declare LC3 codec metadata configuration */
117 #define BT_AUDIO_CODEC_CFG_LC3_META(_stream_context)                                               \
118 	{                                                                                          \
119 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,                         \
120 				    BT_BYTES_LIST_LE16(_stream_context))                           \
121 	}
122 
123 /**
124  * @brief Helper to declare LC3 codec configuration.
125  *
126  * @param _freq            Sampling frequency (``BT_AUDIO_CODEC_CFG_FREQ_*``)
127  * @param _duration        Frame duration (``BT_AUDIO_CODEC_CFG_DURATION_*``)
128  * @param _loc             Audio channel location bitfield (@ref bt_audio_location)
129  * @param _len             Octets per frame (16-bit integer)
130  * @param _frames_per_sdu  Frames per SDU (8-bit integer)
131  * @param _stream_context  Stream context (``BT_AUDIO_CONTEXT_*``)
132  */
133 #define BT_AUDIO_CODEC_LC3_CONFIG(_freq, _duration, _loc, _len, _frames_per_sdu, _stream_context)  \
134 	BT_AUDIO_CODEC_CFG(                                                                        \
135 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,                                          \
136 		BT_AUDIO_CODEC_CFG_LC3_DATA(_freq, _duration, _loc, _len, _frames_per_sdu),        \
137 		BT_AUDIO_CODEC_CFG_LC3_META(_stream_context))
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 /**
144  * @}
145  */
146 
147 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_LC3_H_ */
148