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 #define BTSTACK_FILE__ "btstack_sbc_encoder_bluedroid.c" 39 40 // ***************************************************************************** 41 // 42 // SBC encoder based on Bluedroid library 43 // 44 // ***************************************************************************** 45 46 #include "btstack_config.h" 47 48 #include <stdint.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include "btstack_sbc.h" 53 #include "btstack_sbc_plc.h" 54 #include "btstack_debug.h" 55 #include "btstack_util.h" 56 57 #include "sbc_encoder.h" 58 59 #define mSBC_SYNCWORD 0xad 60 #define SBC_SYNCWORD 0x9c 61 #define SBC_MAX_CHANNELS 2 62 // #define LOG_FRAME_STATUS 63 64 65 typedef struct { 66 SBC_ENC_PARAMS context; 67 int num_data_bytes; 68 uint8_t sbc_packet[1000]; 69 } bludroid_encoder_state_t; 70 71 static btstack_sbc_encoder_state_t * sbc_encoder_state_singleton = NULL; 72 static bludroid_encoder_state_t bd_encoder_state; 73 74 75 void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode, 76 int blocks, int subbands, btstack_sbc_allocation_method_t allocation_method, 77 int sample_rate, int bitpool, btstack_sbc_channel_mode_t channel_mode){ 78 79 if (sbc_encoder_state_singleton && (sbc_encoder_state_singleton != state) ){ 80 log_error("SBC encoder: different sbc decoder state is allready registered"); 81 } 82 83 sbc_encoder_state_singleton = state; 84 85 if (!sbc_encoder_state_singleton){ 86 log_error("SBC encoder init: sbc state is NULL"); 87 } 88 89 sbc_encoder_state_singleton->mode = mode; 90 91 switch (sbc_encoder_state_singleton->mode){ 92 case SBC_MODE_STANDARD: 93 bd_encoder_state.context.s16NumOfBlocks = blocks; 94 bd_encoder_state.context.s16NumOfSubBands = subbands; 95 bd_encoder_state.context.s16AllocationMethod = (uint8_t)allocation_method; 96 bd_encoder_state.context.s16BitPool = bitpool; 97 bd_encoder_state.context.mSBCEnabled = 0; 98 bd_encoder_state.context.s16ChannelMode = (uint8_t)channel_mode; 99 bd_encoder_state.context.s16NumOfChannels = 2; 100 if (bd_encoder_state.context.s16ChannelMode == SBC_MONO){ 101 bd_encoder_state.context.s16NumOfChannels = 1; 102 } 103 switch(sample_rate){ 104 case 16000: bd_encoder_state.context.s16SamplingFreq = SBC_sf16000; break; 105 case 32000: bd_encoder_state.context.s16SamplingFreq = SBC_sf32000; break; 106 case 44100: bd_encoder_state.context.s16SamplingFreq = SBC_sf44100; break; 107 case 48000: bd_encoder_state.context.s16SamplingFreq = SBC_sf48000; break; 108 default: bd_encoder_state.context.s16SamplingFreq = 0; break; 109 } 110 break; 111 case SBC_MODE_mSBC: 112 bd_encoder_state.context.s16NumOfBlocks = 15; 113 bd_encoder_state.context.s16NumOfSubBands = 8; 114 bd_encoder_state.context.s16AllocationMethod = SBC_LOUDNESS; 115 bd_encoder_state.context.s16BitPool = 26; 116 bd_encoder_state.context.s16ChannelMode = SBC_MONO; 117 bd_encoder_state.context.s16NumOfChannels = 1; 118 bd_encoder_state.context.mSBCEnabled = 1; 119 bd_encoder_state.context.s16SamplingFreq = SBC_sf16000; 120 break; 121 default: 122 btstack_assert(false); 123 break; 124 } 125 bd_encoder_state.context.pu8Packet = bd_encoder_state.sbc_packet; 126 127 sbc_encoder_state_singleton->encoder_state = &bd_encoder_state; 128 SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context; 129 SBC_Encoder_Init(context); 130 } 131 132 133 void btstack_sbc_encoder_process_data(int16_t * input_buffer){ 134 if (!sbc_encoder_state_singleton){ 135 log_error("SBC encoder: sbc state is NULL, call btstack_sbc_encoder_init to initialize it"); 136 } 137 SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context; 138 context->ps16PcmBuffer = input_buffer; 139 if (context->mSBCEnabled){ 140 context->pu8Packet[0] = 0xad; 141 } 142 SBC_Encoder(context); 143 } 144 145 int btstack_sbc_encoder_num_audio_frames(void){ 146 SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context; 147 return context->s16NumOfSubBands * context->s16NumOfBlocks; 148 } 149 150 uint8_t * btstack_sbc_encoder_sbc_buffer(void){ 151 SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context; 152 return context->pu8Packet; 153 } 154 155 uint16_t btstack_sbc_encoder_sbc_buffer_length(void){ 156 SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context; 157 return context->u16PacketLength; 158 } 159