xref: /btstack/src/btstack_lc3.h (revision 65113a0c455b2dca58bc1507bcd217185db35d47)
1 /*
2  * Copyright (C) 2022 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  *
17  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
21  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /**
33  * @title LC3 Interface
34  *
35  * Interface for LC3 implementations
36  *
37  */
38 
39 #ifndef BTSTACK_LC3_H
40 #define BTSTACK_LC3_H
41 
42 #include <stdint.h>
43 
44 #if defined __cplusplus
45 extern "C" {
46 #endif
47 
48 /* API_START */
49 
50 typedef enum {
51     BTSTACK_LC3_FRAME_DURATION_10000US,
52     BTSTACK_LC3_FRAME_DURATION_7500US
53 } btstack_lc3_frame_duration_t;
54 
55 typedef struct {
56 
57     /**
58      * Configure Decoder
59      * @param context
60      * @param sample_rate
61      * @param frame_duration
62      * @param octets_per_frame
63      * @return status
64      */
65     uint8_t (*configure)(void * context, uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame);
66 
67     /**
68      * Get number of octets per LC3 frame for bitrate
69      * @param context
70      * @param bitrate
71      * @return octets_per_frame
72      */
73     uint16_t (*get_number_octets_for_bitrate)(void * context, uint32_t bitrate);
74 
75     /**
76      * Get number of samples per LC3 frame
77      * @param context
78      * @return number of samples
79      */
80     uint16_t (*get_number_samples_per_frame)(void * context);
81 
82     /**
83      * Decode LC3 Frame into signed 16-bit samples
84      * @param context
85      * @param bytes
86      * @param BFI Bad Frame Indication flags
87      * @param pcm_out buffer for decoded PCM samples
88      * @param stride count between two consecutive samples
89      * @param BEC_detect Bit Error Detected flag
90      * @return status
91      */
92      uint8_t (*decode_signed_16)(void * context, const uint8_t *bytes, uint8_t BFI,
93                        int16_t* pcm_out, uint16_t stride, uint8_t * BEC_detect);
94 
95     /**
96      * Decode LC3 Frame into signed 24-bit samples, sign-extended to 32-bit
97      * @param context
98      * @param bytes
99      * @param BFI Bad Frame Indication flags
100      * @param pcm_out buffer for decoded PCM samples
101      * @param stride count between two consecutive samples
102      * @param BEC_detect Bit Error Detected flag
103      * @return status
104      */
105     uint8_t (*decode_signed_24)(void * context, const uint8_t *bytes, uint8_t BFI,
106                                 int32_t* pcm_out, uint16_t stride, uint8_t * BEC_detect);
107 
108 } btstack_lc3_decoder_t;
109 
110 typedef struct {
111     /**
112      * Configure Decoder
113      * @param context
114      * @param sample_rate
115      * @param frame_duration
116      * @param octets_per_frame
117      * @return status
118      */
119     uint8_t (*configure)(void * context, uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame);
120 
121     /**
122      * Get bitrate from number of octets per LC3 frame
123      * @param context
124      * @param number_of_octets
125      * @return bitrate
126      */
127     uint32_t (*get_bitrate_for_number_of_octets)(void * context, uint16_t number_of_octets);
128 
129     /**
130      * Get number of samples per LC3 frame
131      * @param context
132     * @return number of samples
133     */
134     uint16_t (*get_number_samples_per_frame)(void * context);
135 
136     /**
137      * Encode LC3 Frame with 16-bit signed PCM samples
138      * @param context
139      * @param pcm_in buffer for decoded PCM samples
140      * @param stride count between two consecutive samples
141      * @param bytes
142      * @return status
143      */
144     uint8_t (*encode_signed_16)(void * context, const int16_t* pcm_in, uint16_t stride, uint8_t *bytes);
145 
146     /**
147      * Encode LC3 Frame with 24-bit signed PCM samples, sign-extended to 32 bit
148      * @param context
149      * @param pcm_in buffer for decoded PCM samples
150      * @param stride count between two consecutive samples
151      * @param bytes
152      * @return status
153      */
154     uint8_t (*encode_signed_24)(void * context, const int32_t* pcm_in, uint16_t stride, uint8_t *bytes);
155 
156 } btstack_lc3_encoder_t;
157 
158 /**
159  * @brief Map enum to ISO Interval in us
160  * @param frame_duration enum
161  * @return frame_duratoin in us
162  */
163 uint16_t btstack_lc3_frame_duration_in_us(btstack_lc3_frame_duration_t frame_duration);
164 
165 /**
166  * @bbrief Calculate number of samples per ISO Interval
167  * @param sample_rate
168  * @param frame_duration
169  * @return
170  */
171 uint16_t btstack_lc3_samples_per_frame(uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration);
172 
173 /* API_END */
174 
175 #if defined __cplusplus
176 }
177 #endif
178 #endif // LC3_H
179