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 /** 161 * @brief Init SBC decoder 162 * @deprecated Please use btstack_sbc_decoder_bluedroid_init_instance() from btstack_sbc_bluedroid.h and call btstack_sbc_decoder->configure() instead 163 * @param state 164 * @param mode 165 * @param callback for decoded PCM data in host endianess 166 * @param context provided in callback 167 */ 168 169 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); 170 171 /** 172 * @brief Process received SBC data 173 * @deprecated Please use btstack_sbc_decoder->decode_signed_16() instead 174 * @param state 175 * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost 176 * @param buffer 177 * @param size 178 */ 179 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, const uint8_t * buffer, int size); 180 181 /** 182 * @brief Get number of samples per SBC frame 183 * @deprecated Please use btstack_sbc_decoder->num_samples_per_frame() instead 184 */ 185 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state); 186 187 /** 188 * @brief Get number of channels 189 * @deprecated Please use btstack_sbc_decoder->num_channels() instead 190 */ 191 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state); 192 193 /** 194 * @brief Get sample rate in hz 195 * @deprecated Please use btstack_sbc_decoder->sample_rate() instead 196 */ 197 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state); 198 199 200 /* BTstack SBC Encoder */ 201 /** 202 * @brief Init SBC encoder 203 * @deprecated Please use btstack_sbc_encoder_bluedroid_init_instance() from btstack_sbc_bluedroid.h and call btstack_sbc_encoder->configure() instead 204 * @param state 205 * @param mode 206 * @param blocks 207 * @param subbands 208 * @param allocation_method 209 * @param sample_rate 210 * @param bitpool 211 * @param channel_mode 212 */ 213 void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode, 214 int blocks, int subbands, btstack_sbc_allocation_method_t allocation_method, 215 int sample_rate, int bitpool, btstack_sbc_channel_mode_t channel_mode); 216 217 /** 218 * @brief Encode PCM data 219 * @deprecated Please use btstack_sbc_encoder->encode_signed_16() instead 220 * @param buffer with samples in host endianess 221 */ 222 void btstack_sbc_encoder_process_data(int16_t * input_buffer); 223 224 /** 225 * @brief Return SBC frame 226 * @deprecated not needed when using the codeec instance interface 227 */ 228 uint8_t * btstack_sbc_encoder_sbc_buffer(void); 229 230 /** 231 * @brief Return SBC frame length 232 * @deprecated Please use btstack_sbc_encoder->encode_signed_16() instead 233 */ 234 uint16_t btstack_sbc_encoder_sbc_buffer_length(void); 235 236 /** 237 * @brief Return number of audio frames required for one SBC packet 238 * @deprecated Please use btstack_sbc_encoder->encode_signed_16() instead 239 * @note each audio frame contains 2 sample values in stereo modes 240 */ 241 int btstack_sbc_encoder_num_audio_frames(void); 242 243 /* API_END */ 244 245 // testing only 246 void btstack_sbc_decoder_test_set_plc_enabled(int plc_enabled); 247 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period); 248 249 #if defined __cplusplus 250 } 251 #endif 252 253 #endif // BTSTACK_SBC_H 254