xref: /btstack/src/btstack_lc3.h (revision 98f1394d9d4c33f501ec5dcff54d145a92013c5e)
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 context
63      * @return status
64      */
65     uint8_t (*configure)(void * context, uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration);
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 byte_count
87      * @param BFI Bad Frame Indication flags
88      * @param pcm_out buffer for decoded PCM samples
89      * @param stride count between two consecutive samples
90      * @param BEC_detect Bit Error Detected flag
91      * @return status
92      */
93      uint8_t (*decode_signed_16)(void * context, const uint8_t *bytes, uint16_t byte_count, uint8_t BFI,
94                        int16_t* pcm_out, uint16_t stride, uint8_t * BEC_detect);
95 
96     /**
97      * Decode LC3 Frame into signed 24-bit samples, sign-extended to 32-bit
98      * @param context
99      * @param bytes
100      * @param byte_count
101      * @param BFI Bad Frame Indication flags
102      * @param pcm_out buffer for decoded PCM samples
103      * @param stride count between two consecutive samples
104      * @param BEC_detect Bit Error Detected flag
105      * @return status
106      */
107     uint8_t (*decode_signed_24)(void * context, const uint8_t *bytes, uint16_t byte_count, uint8_t BFI,
108                                 int32_t* pcm_out, uint16_t stride, uint8_t * BEC_detect);
109 
110 } btstack_lc3_decoder_t;
111 
112 typedef struct {
113     /**
114      * Configure Decoder
115      * @param context
116      * @param sample_rate
117      * @param frame_duration
118      * @param context
119      * @return status
120      */
121     uint8_t (*configure)(void * context, uint32_t sample_rate, btstack_lc3_frame_duration_t frame_duration);
122 
123     /**
124      * Get bitrate from number of octets per LC3 frame
125      * @param context
126      * @param number_of_octets
127      * @return bitrate
128      */
129     uint32_t (*get_bitrate_for_number_of_octets)(void * context, uint16_t number_of_octets);
130 
131     /**
132      * Get number of samples per LC3 frame
133      * @param context
134     * @return number of samples
135     */
136     uint16_t (*get_number_samples_per_frame)(void * context);
137 
138     /**
139      * Encode LC3 Frame with 16-bit signed PCM samples
140      * @param context
141      * @param pcm_in buffer for decoded PCM samples
142      * @param stride count between two consecutive samples
143      * @param bytes
144      * @param byte_count
145      * @return status
146      */
147     uint8_t (*encode_signed_16)(void * context, const int16_t* pcm_in, uint16_t stride, uint8_t *bytes, uint16_t byte_count);
148 
149     /**
150      * Encode LC3 Frame with 24-bit signed PCM samples, sign-extended to 32 bit
151      * @param context
152      * @param pcm_in buffer for decoded PCM samples
153      * @param stride count between two consecutive samples
154      * @param bytes
155      * @param byte_count
156      * @return status
157      */
158     uint8_t (*encode_signed_24)(void * context, const int32_t* pcm_in, uint16_t stride, uint8_t *bytes, uint16_t byte_count);
159 
160 } btstack_lc3_encoder_t;
161 
162 /* API_END */
163 
164 #if defined __cplusplus
165 }
166 #endif
167 #endif // LC3_H
168