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 /* API_START */ 91 92 /* BTstack SBC decoder */ 93 /** 94 * @brief Init SBC decoder 95 * @param state 96 * @param mode 97 * @param callback for decoded PCM data in host endianess 98 * @param context provided in callback 99 */ 100 101 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); 102 103 /** 104 * @brief Process received SBC data 105 * @param state 106 * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost 107 * @param buffer 108 * @param size 109 */ 110 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, const uint8_t * buffer, int size); 111 112 /** 113 * @brief Get number of samples per SBC frame 114 */ 115 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state); 116 117 /* 118 * @brief Get number of channels 119 */ 120 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state); 121 122 /* 123 * @brief Get sample rate in hz 124 */ 125 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state); 126 127 128 /* BTstack SBC Encoder */ 129 /** 130 * @brief Init SBC encoder 131 * @param state 132 * @param mode 133 * @param blocks 134 * @param subbands 135 * @param allocation_method 136 * @param sample_rate 137 * @param bitpool 138 * @param channel_mode 139 */ 140 void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode, 141 int blocks, int subbands, btstack_sbc_allocation_method_t allocation_method, 142 int sample_rate, int bitpool, btstack_sbc_channel_mode_t channel_mode); 143 144 /** 145 * @brief Encode PCM data 146 * @param buffer with samples in host endianess 147 */ 148 void btstack_sbc_encoder_process_data(int16_t * input_buffer); 149 150 /** 151 * @brief Return SBC frame 152 */ 153 uint8_t * btstack_sbc_encoder_sbc_buffer(void); 154 155 /** 156 * @brief Return SBC frame length 157 */ 158 uint16_t btstack_sbc_encoder_sbc_buffer_length(void); 159 160 /** 161 * @brief Return number of audio frames required for one SBC packet 162 * @note each audio frame contains 2 sample values in stereo modes 163 */ 164 int btstack_sbc_encoder_num_audio_frames(void); 165 166 /* API_END */ 167 168 // testing only 169 void btstack_sbc_decoder_test_set_plc_enabled(int plc_enabled); 170 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period); 171 172 #if defined __cplusplus 173 } 174 #endif 175 176 #endif // BTSTACK_SBC_H 177