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