xref: /btstack/src/classic/btstack_sbc.h (revision 1b464e99afd70ddaf6b75be1ba7cc563a5f5dfd8)
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 /*
39  * btstack_sbc.h
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,
55     SBC_MODE_mSBC
56 } btstack_sbc_mode_t;
57 
58 typedef struct {
59     void * context;
60     void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
61     // private
62     void * decoder_state;
63     btstack_sbc_plc_state_t plc_state;
64     btstack_sbc_mode_t mode;
65 
66     // summary of processed good, bad and zero frames
67     int good_frames_nr;
68     int bad_frames_nr;
69     int zero_frames_nr;
70 } btstack_sbc_decoder_state_t;
71 
72 typedef struct {
73     // private
74     void * encoder_state;
75     btstack_sbc_mode_t mode;
76 } btstack_sbc_encoder_state_t;
77 
78 /* API_START */
79 
80 /* BTstack SBC decoder */
81 /**
82  * @brief Init SBC decoder
83  * @param state
84  * @param mode
85  * @param callback for decoded PCM data in host endianess
86  * @param context provided in callback
87  */
88 
89 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);
90 
91 /**
92  * @brief Process received SBC data
93  * @param state
94  * @param packet_status_flag from SCO packet: 0 = OK, 1 = possibly invalid data, 2 = no data received, 3 = data partially lost
95  * @param buffer
96  * @param size
97  */
98 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size);
99 
100 /**
101  * @brief Get number of samples per SBC frame
102  */
103 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state);
104 
105 /*
106  * @brief Get number of channels
107  */
108 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state);
109 
110 /*
111  * @brief Get sample rate in hz
112  */
113 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
114 
115 
116 /* BTstack SBC Encoder */
117 /**
118  * @brief Init SBC encoder
119  * @param state
120  * @param mode
121  * @param blocks
122  * @param subbands
123  * @param allocation_method
124  * @param sample_rate
125  * @param bitpool
126  * @param channel_mode
127  */
128 void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
129                         int blocks, int subbands, int allocation_method, int sample_rate, int bitpool, int channel_mode);
130 
131 /**
132  * @brief Encode PCM data
133  * @param buffer with samples in host endianess
134  */
135 void btstack_sbc_encoder_process_data(int16_t * input_buffer);
136 
137 /**
138  * @brief Return SBC frame
139  */
140 uint8_t * btstack_sbc_encoder_sbc_buffer(void);
141 
142 /**
143  * @brief Return SBC frame length
144  */
145 uint16_t  btstack_sbc_encoder_sbc_buffer_length(void);
146 
147 /**
148  * @brief Return number of audio frames required for one SBC packet
149  * @note  each audio frame contains 2 sample values in stereo modes
150  */
151 int  btstack_sbc_encoder_num_audio_frames(void);
152 
153 /* API_END */
154 
155 // testing only
156 void btstack_sbc_decoder_test_set_plc_enabled(int plc_enabled);
157 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period);
158 
159 #if defined __cplusplus
160 }
161 #endif
162 
163 #endif // BTSTACK_SBC_H
164