xref: /btstack/src/classic/btstack_sbc_encoder_bluedroid.c (revision 73cbd4d97f92d480088752e70324cf243f97d616)
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_bludroid.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 <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "btstack_sbc.h"
54 #include "btstack_sbc_plc.h"
55 
56 #include "sbc_encoder.h"
57 #include "btstack.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, int allmethod, int sample_rate, int bitpool, int channel_mode){
77 
78     if (sbc_encoder_state_singleton && sbc_encoder_state_singleton != state ){
79         log_error("SBC encoder: different sbc decoder state is allready registered");
80     }
81 
82     sbc_encoder_state_singleton = state;
83 
84     if (!sbc_encoder_state_singleton){
85         log_error("SBC encoder init: sbc state is NULL");
86     }
87 
88     sbc_encoder_state_singleton->mode = mode;
89 
90     switch (sbc_encoder_state_singleton->mode){
91         case SBC_MODE_STANDARD:
92             bd_encoder_state.context.s16NumOfBlocks = blocks;
93             bd_encoder_state.context.s16NumOfSubBands = subbands;
94             bd_encoder_state.context.s16AllocationMethod = allmethod;
95             bd_encoder_state.context.s16BitPool = bitpool;
96             bd_encoder_state.context.mSBCEnabled = 0;
97             bd_encoder_state.context.s16ChannelMode = channel_mode;
98             bd_encoder_state.context.s16NumOfChannels = 2;
99             if (bd_encoder_state.context.s16ChannelMode == SBC_MONO){
100                 bd_encoder_state.context.s16NumOfChannels = 1;
101             }
102             switch(sample_rate){
103                 case 16000: bd_encoder_state.context.s16SamplingFreq = SBC_sf16000; break;
104                 case 32000: bd_encoder_state.context.s16SamplingFreq = SBC_sf32000; break;
105                 case 44100: bd_encoder_state.context.s16SamplingFreq = SBC_sf44100; break;
106                 case 48000: bd_encoder_state.context.s16SamplingFreq = SBC_sf48000; break;
107                 default: bd_encoder_state.context.s16SamplingFreq = 0; break;
108             }
109             break;
110         case SBC_MODE_mSBC:
111             bd_encoder_state.context.s16NumOfBlocks    = 15;
112             bd_encoder_state.context.s16NumOfSubBands  = 8;
113             bd_encoder_state.context.s16AllocationMethod = SBC_LOUDNESS;
114             bd_encoder_state.context.s16BitPool   = 26;
115             bd_encoder_state.context.s16ChannelMode = SBC_MONO;
116             bd_encoder_state.context.s16NumOfChannels = 1;
117             bd_encoder_state.context.mSBCEnabled = 1;
118             bd_encoder_state.context.s16SamplingFreq = SBC_sf16000;
119             break;
120     }
121     bd_encoder_state.context.pu8Packet = bd_encoder_state.sbc_packet;
122 
123     sbc_encoder_state_singleton->encoder_state = &bd_encoder_state;
124     SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context;
125     SBC_Encoder_Init(context);
126 }
127 
128 
129 void btstack_sbc_encoder_process_data(int16_t * input_buffer){
130     if (!sbc_encoder_state_singleton){
131         log_error("SBC encoder: sbc state is NULL, call btstack_sbc_encoder_init to initialize it");
132     }
133     SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context;
134     context->ps16PcmBuffer = input_buffer;
135     if (context->mSBCEnabled){
136         context->pu8Packet[0] = 0xad;
137     }
138     SBC_Encoder(context);
139 }
140 
141 int btstack_sbc_encoder_num_audio_frames(void){
142     SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context;
143     return context->s16NumOfSubBands * context->s16NumOfBlocks;
144 }
145 
146 uint8_t * btstack_sbc_encoder_sbc_buffer(void){
147     SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context;
148     return context->pu8Packet;
149 }
150 
151 uint16_t  btstack_sbc_encoder_sbc_buffer_length(void){
152     SBC_ENC_PARAMS * context = &((bludroid_encoder_state_t *)sbc_encoder_state_singleton->encoder_state)->context;
153     return context->u16PacketLength;
154 }
155