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