xref: /btstack/src/classic/btstack_sbc.h (revision 1ca6167147c5c2942d33b7d999a31b76229f5c56)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * SBC
40  *
41  */
42 
43 #ifndef BTSTACK_SBC_H
44 #define BTSTACK_SBC_H
45 
46 #include <stdint.h>
47 #include "btstack_sbc_plc.h"
48 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 typedef enum {
54     SBC_MODE_STANDARD = 0,
55     SBC_MODE_mSBC
56 } btstack_sbc_mode_t;
57 
58 typedef enum {
59     SBC_CHANNEL_MODE_MONO = 0,
60     SBC_CHANNEL_MODE_DUAL_CHANNEL,
61     SBC_CHANNEL_MODE_STEREO,
62     SBC_CHANNEL_MODE_JOINT_STEREO
63 } btstack_sbc_channel_mode_t;
64 
65 typedef enum {
66     SBC_ALLOCATION_METHOD_LOUDNESS = 0,
67     SBC_ALLOCATION_METHOD_SNR
68 } btstack_sbc_allocation_method_t;
69 
70 typedef struct {
71     void * context;
72     void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
73     // private
74     void * decoder_state;
75     btstack_sbc_plc_state_t plc_state;
76     btstack_sbc_mode_t mode;
77 
78     // summary of processed good, bad and zero frames
79     int good_frames_nr;
80     int bad_frames_nr;
81     int zero_frames_nr;
82 } btstack_sbc_decoder_state_t;
83 
84 typedef struct {
85     // private
86     void * encoder_state;
87     btstack_sbc_mode_t mode;
88 } btstack_sbc_encoder_state_t;
89 
90 typedef struct {
91     /**
92      * Configure Encoder
93      * @param encoder_context
94      * @param mode
95      * @param blocks
96      * @param subbands
97      * @param allocation_method
98      * @param sample_rate
99      * @param bitpool
100      * @param channel_mode
101      * @return status
102      */
103     uint8_t (*configure)(void * encoder_context, btstack_sbc_mode_t mode,
104                          uint8_t blocks, uint8_t subbands, btstack_sbc_allocation_method_t allocation_method,
105                          uint16_t sample_rate, uint8_t bitpool, btstack_sbc_channel_mode_t channel_mode);
106 
107     /**
108      * @brief Return number of audio frames required for one SBC packet
109      * @param encoder_context
110      * @note  each audio frame contains 2 sample values in stereo modes
111      */
112     uint16_t (*num_audio_frames)(void * encoder_context);
113 
114     /**
115      * @brief Return SBC frame length
116      * @param encoder_context
117      */
118     uint16_t (*sbc_buffer_length)(void * encoder_context);
119 
120     /**
121      * @brief Encode PCM data
122      * @param encoder_context
123      * @param pcm_in with samples in host endianess
124      * @param sbc_out
125      * @return status
126      */
127     uint8_t (*encode_signed_16)(void * encoder_context, const int16_t* pcm_in, uint8_t * sbc_out);
128 
129 } btstack_sbc_encoder_t;
130 
131 typedef struct {
132     /**
133      * @brief Init SBC decoder
134      * @param decoder_context
135      * @param mode
136      * @param callback_handler for decoded PCM data in host endianess
137      * @param callback_context provided as context in call to callback_handler
138      */
139     void (*configure)(void * decoder_context,
140             btstack_sbc_mode_t mode,
141             void (*callback_handler)(int16_t * data, int num_samples, int num_channels,
142                              int sample_rate, void * context),
143             void * callback_context);
144 
145     /**
146      * @brief Process received SBC data and deliver pcm via context_callback
147      * @param decoder_context
148      * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
149      * @param buffer
150      * @param size
151      */
152     void (*decode_signed_16)(void * decoder_context, uint8_t packet_status_flag, const uint8_t * buffer, uint16_t size);
153 
154  } btstack_sbc_decoder_t;
155 
156 /* API_START */
157 
158 /* BTstack SBC decoder */
159 /**
160  * @brief Init SBC decoder
161  * @param state
162  * @param mode
163  * @param callback for decoded PCM data in host endianess
164  * @param context provided in callback
165  */
166 
167 void btstack_sbc_decoder_init(btstack_sbc_decoder_state_t * state, btstack_sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context);
168 
169 /**
170  * @brief Process received SBC data
171  * @param state
172  * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
173  * @param buffer
174  * @param size
175  */
176 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, const uint8_t * buffer, int size);
177 
178 /**
179  * @brief Get number of samples per SBC frame
180  */
181 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state);
182 
183 /*
184  * @brief Get number of channels
185  */
186 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state);
187 
188 /*
189  * @brief Get sample rate in hz
190  */
191 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
192 
193 
194 /* BTstack SBC Encoder */
195 /**
196  * @brief Init SBC encoder
197  * @param state
198  * @param mode
199  * @param blocks
200  * @param subbands
201  * @param allocation_method
202  * @param sample_rate
203  * @param bitpool
204  * @param channel_mode
205  */
206 void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
207                         int blocks, int subbands, btstack_sbc_allocation_method_t allocation_method,
208                         int sample_rate, int bitpool, btstack_sbc_channel_mode_t channel_mode);
209 
210 /**
211  * @brief Encode PCM data
212  * @param buffer with samples in host endianess
213  */
214 void btstack_sbc_encoder_process_data(int16_t * input_buffer);
215 
216 /**
217  * @brief Return SBC frame
218  */
219 uint8_t * btstack_sbc_encoder_sbc_buffer(void);
220 
221 /**
222  * @brief Return SBC frame length
223  */
224 uint16_t  btstack_sbc_encoder_sbc_buffer_length(void);
225 
226 /**
227  * @brief Return number of audio frames required for one SBC packet
228  * @note  each audio frame contains 2 sample values in stereo modes
229  */
230 int  btstack_sbc_encoder_num_audio_frames(void);
231 
232 /* API_END */
233 
234 // testing only
235 void btstack_sbc_decoder_test_set_plc_enabled(int plc_enabled);
236 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period);
237 
238 #if defined __cplusplus
239 }
240 #endif
241 
242 #endif // BTSTACK_SBC_H
243