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